C++ template specialization, calling methods on types that could be pointers or references unambiguously

Small overloaded functions can be used to turn reference into pointer: template<typename T> T * ptr(T & obj) { return &obj; } //turn reference into pointer! template<typename T> T * ptr(T * obj) { return obj; } //obj is already pointer, return it! Now instead of doing this: if(elem->Intersects(_bounds) == false) return false; if(elem.Intersects(_bounds) == … Read more

How to know if a type is a specialization of std::vector?

In C++11 you can also do it in a more generic way: #include <type_traits> #include <iostream> #include <vector> #include <list> template<typename Test, template<typename…> class Ref> struct is_specialization : std::false_type {}; template<template<typename…> class Ref, typename… Args> struct is_specialization<Ref<Args…>, Ref>: std::true_type {}; int main() { typedef std::vector<int> vec; typedef int not_vec; std::cout << is_specialization<vec, std::vector>::value << is_specialization<not_vec, … Read more

What is the correct syntax for defining a specialization of a function template?

Here are comments with each syntax: void foo(int param); //not a specialization, it is an overload void foo<int>(int param); //ill-formed //this form always works template <> void foo<int>(int param); //explicit specialization //same as above, but works only if template argument deduction is possible! template <> void foo(int param); //explicit specialization //same as above, but works … Read more

Should you prefer overloading over specialization of function templates?

Short story: overload when you can, specialise when you need to. Long story: C++ treats specialisation and overloads very differently. This is best explained with an example. template <typename T> void foo(T); template <typename T> void foo(T*); // overload of foo(T) template <> void foo<int>(int*); // specialisation of foo(T*) foo(new int); // calls foo<int>(int*); Now … Read more

Why can function templates not be partially specialized?

AFAIK that’s changed in C++0x. I guess it was just an oversight (considering that you can always get the partial specialization effect with more verbose code, by placing the function as a static member of a class). You might look up the relevant DR (Defect Report), if there is one. EDIT: checking this, I find … Read more

Why can I seemingly define a partial specialization for function templates?

Function partial specialization is not allowed yet as per the standard. In the example, you are actually overloading & not specializing the max<T1,T2> function. Its syntax should have looked somewhat like below, had it been allowed: // Partial specialization is not allowed by the spec, though! template <typename T> inline T const& max<T,T> (T const& … Read more

Is std::vector a `user-defined type`?

Prof. Stroustrup is very clear that any type that is not built-in is user-defined. See the second paragraph of section 9.1 in Programming Principles and Practice Using C++. He even specifically calls out “standard library types” as an example of user-defined types. In other words, a user-defined type is any compound type. Source The article … Read more

C++ specialization of template function inside template class

AFAIK (and the standards experts can correct me), you cannot specialize a templated function of a class template without specializing the class itself… i.e. the following I think will work: template <> template <> int X<Y>::getAThing<int>(std::string param) { return getIntThing(param); // Some function that crunches on param and returns an int. }

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)