FAQ Database Discussion Community


Conditional template specialization on method of a non template class

c++,templates,boost,template-specialization,enable-if
I am trying to use boost::enable_if to conditional specialize a method of a non-templated class, but failing at it. //OSSpecific.h ... //If some OS typedef unsigned int UINT; //else typedef unsigned long long UINT; ... //Foo.h ... #include <OSSpecific.h> ... class Foo { public: ... template <typename T> returnThis<T>* bar();...

Confused by Tuple example in TC++PL 4th Edition

c++,templates,template-specialization
I'm having trouble working out exactly how this code works. I have a shorter version of the example here, that I think qualifies for fair use: #include <typeinfo> #include <iostream> using namespace std; struct Nil{}; // Primary template. template<typename T1=Nil, typename T2=Nil> struct Tuple : Tuple<T2> { T1 x; using...

Define virtual function to be pure in special cases of templated class

c++,c++14,template-specialization,pure-virtual
I am trying to define a templated base class that defines a virtual interface to be overriden by the user. In some cases it would make sense to have a default implementation of this method but that isn't true for all type parameters. Consider this: template<typename T> struct supplier {...

Out-of-class definition of function of specialized inner class template?

c++,templates,c++11,c++14,template-specialization
Please consider the following ill-formed program: struct S { template<class T> struct J { }; }; template<> struct S::J<void> { void f(); }; template<> void S::J<void>::f() {} // ERROR $ clang++ -std=c++11 test.cpp no function template matches function template specialization 'f' $ g++ -std=c++11 test.cpp template-id ‘f<>’ for ‘void S::J<void>::f()’...

Template specialization does not do what I need

c++,templates,template-specialization
I am new in C++ templates. I have a template struct AAA and it has a field called field. When some one call it with int like AAA<int>, I want to have additional field called second_field. Could some one tell me how to fix this code? template<typename T> struct AAA...

Why does using the scope resolution operator change which overloaded template in the global namespace gets called?

c++,templates,template-specialization,overload-resolution
Consider the following code: #include <iostream> template <class W, class T> void foo(W& a, T& t) { std::cout << "generic" << std::endl; } template <template <bool> class W, class T> void foo(W<true>& a, const T& t) { foo(a, const_cast<T&>(t)); } template <class W> void foo(W& a, int& t) { std::cout...

What does SFINAE not work correctly with following has_member function?

c++,templates,c++11,template-specialization,sfinae
I'm trying out examples from Walter Brown's TMP talk and I'm trying to get his has_member implementation working. However the implementation seems to falsely return true which leads me to believe there is some detail of SFINAE that I am not understanding. #include <iostream> #include <type_traits> template <class ...> using...

Specialization of a class template with the given class

c++,templates,template-specialization
I have a class A and a class template B declared as follows: class A; template <class T> class B; I want to declare a specialization of B for T=int, which coincides with A, i.e. something like this template<> class B<int> = A; ...

template template class specialization

c++,templates,template-specialization
I am just learning about Template Template class specialisation. Not a big problem to explain in detail. From my understanding std::uniform_int_distribution is a template whereas std::uniform_int_distribution<Type> is the full specialisation of uniform_int_distribution giving a type. I pass this in the specialisation class template as follows below Main class template <template...

method template specialization by return type

c++,templates,template-specialization
I have a class with template method that the parameter defines the return type. The default constructor for everything else is ok, but for bool I would like the method to return true. I am trying to specialize it as the following code, but this does not compile. class Foo...

How can a C++ template be specialized for all 32-bit POD types?

c++,templates,endianness,template-specialization,specialization
I've developed a simple template function for swapping the byte order of a single field: template <typename T> inline void SwapEndian(T& ptr) { char *bytes = reinterpret_cast<char*>(&ptr); int a = sizeof(T) / 2; while (a--) { char tmp = bytes[a]; int b = sizeof(T) - 1 - a; bytes[a] =...

Forwarding a non-type argument causes different behaviour on Variable Template

c++,c++14,template-specialization,variable-templates
This seems to be another "who's doing it well?" question since gcc 6.0.0 and clang 3.7.0 behaves different. Let's suppose we have a variable template which takes a const char * as non template argument and is specialized for a given pointer: constexpr char INSTANCE_NAME[]{"FOO"}; struct Struct{ void function() const...

static_assert with partial template specialization

c++11,template-specialization,static-assert
template<typename T, typename U = void> struct S { /* static_assert(0, "type unsupported"); */ }; template<typename T> struct S<T, typename std::enable_if<std::is_integral<T>::value, void>::type> { void foo() {} }; ... S<int> i; i.foo(); S<double> d; // d.foo(); I would be expecting that the "master template" would never be instantiated for the case...

Why these C++ cases instantiate different templates

c++,templates,template-specialization,function-signature
I am trying to write some functionality where I need to save different functions and later extract their arguments' types. So I'm using the function signature as template parameter. But I get somewhat unexpected results. Here's the code: #include <functional> #include <iostream> template <class T> struct foo { foo() {...

Find template type of a template type c++

c++,templates,template-specialization,typetraits
I would like to have a function that can take many different things (for simplicity) like so: template <typename T> typename type_to_return<T>::type // <-- Use type_to_return to get result type foo(T t) { return typename type_to_return<T>::type(T); // <-- Build a thing! } I would then specialize the type_to_return class for...

Is a templated and a nontemplated version of the same function considered an overload?

c++,templates,overloading,template-specialization
A very formal question: is this considered an overload? Is removing the template fundamentally different than only overloading on arguments? template<class T> void myFunction(const T& t) {} void myFunction(const double& t) {} Then the follow up question, is it better to follow this approach or to use template specialization, instead...

“template<>” vs “template” without brackets - what's the difference?

c++,templates,template-specialization
Suppose I've defined template <typename T> void foo(T& t); Now, what is the difference between template <> void foo<int>(int& t); and template void foo<int>(int& t); semantically? And do template-with-no-brackets and template-with-empty-brackets have other semantics in other contexts? Related to: How do I force a particular instance of a C++ template...

C++ factoring tempate methods specialization of a template class, is that possible?

c++,templates,refactoring,template-specialization
I have a template method inside a template class. I read that a method can not be specialized without specialize the class before. But I want to factorize some of theses methods, is it possible ? Example : class One { public: static const int number = 1; }; class...

C++14 warning: too many template headers for variable (should be 0)

c++,language-lawyer,c++14,template-specialization,variable-templates
While experimenting with the recent g++-5 compiler, I wrote below statement in a file: template<T> T a; template<> int a = 1; Which results in: warning: too many template headers for a (should be 0) Also effectively, it doesn't really specialize a<int>. e.g. template<typename T> T a; template<> int a...

Distingushing between user and non-user types & templates specialization

c++,templates,overloading,c++14,template-specialization
I need to somehow overload class templates for user and non-user types. The problem arises from the point that template class inherits from the template parameter: #include <iostream> #include<string> #include<map> #include<type_traits> #include<string> using namespace std; class Broken_imput{};//exception class class No_such_class{}; class Io_obj; // For MAP to know about this type...

g++: which ways exist to find out which template specialization has been chosen by compiler?

c++,templates,gcc,template-specialization
In case you're in doubt which specialization of template the compiler has chosen in a specific case - which methods you know how to find out? (preferably without changing the code)

How implicit conversion works for non-type template parameters?

c++,templates,type-conversion,implicit-conversion,template-specialization
I guess (certain) implicit conversions apply when passing non-type template parameters. For example, there should be a conversion from int to std::size_t for expressions like std::array<int, 7>. However, consider the following code: template <bool> void f() { std::cout << "false\n"; } template <> void f<true>() { std::cout << "true\n"; }...

Invalid use of incomplete type for partial template specialization c++

c++,templates,template-specialization
I am trying to specialize a class method foo(). This works well for full template specialization. However, this does not work with partial template specialization. Here the example code which compiles fine on GCC and Clang : #include <iostream> #include <string> template <typename Key, typename Value> struct SimpleKey { Key...

Why specialization based on enable_if is not picked up by compiler

c++,c++11,template-specialization,enable-if
I'd like to specialize class for some type of classes, for example based on std::is_arithmetic. Although compiler doesn't "see" my specialization based on "enable_if" and chooses principle/main template. Could you please help me with this... Below is snippet of code and output after compiling with g++ 4.8 #include < iostream...

Template specialization with std::enable_if<>

c++,templates,c++11,template-specialization
The following code compiles and runs: #include <cinttypes> #include <cstdlib> #include <iostream> #include <limits> #include <sstream> #include <stdexcept> class UnsignedBox { public: typedef std::uint64_t box_type; template<typename UNSIGNED_TYPE, typename std::enable_if< std::numeric_limits<UNSIGNED_TYPE>::is_signed==false && (sizeof(UNSIGNED_TYPE) >= sizeof(UnsignedBox::box_type)), int>::type = 0 > UNSIGNED_TYPE toUnsigned()const { //We've established we're not returning a...

Template specialization: ‘Scalar’ does not name a type

c++,templates,c++11,c++14,template-specialization
I'm trying to implement a template specialization for complex scalars and, with the help of Stackoverflow, got to using std::enable_if_t and its poor man's version #include <type_traits> #include <complex> // declarations namespace Test { template<class Scalar> class A { public: A(const Scalar z); Scalar realPart(); private: Scalar z_; }; }...

Template class specialization function call. C++

c++,templates,template-specialization,specialization
consider this code. template<class T> class A { public: void f(){..} void h(){..} }; template<> class A<int> { public: void f(){// something different..} //void h(){..} }; int main() { A<int> obj; obj.h(); // I want to call A<T>::h(), but compiler erred that there is no h function in A<int> }...

C++ template specialization problems

c++,templates,template-specialization
I have problems with specialized function templates in C++. I'm writing a comparison function template which will be used in sorting different data types. Here is part of my code: template < typename T > bool cmp(T a, T b) { std::cout<<"normal"<<std::endl; return(a >= b); } template < typename T...

Serialization member by member

c++,templates,serialization,template-specialization
I've implemented a template<typename T> Serializer that works on any trivially copyable object of type T, just serializing sizeof(T) bytes. Then I've implemented a couple of (partial) specializations for other types of interest, like std::vector<T> and std::bacis_string<T>. For other types, I trigger a static_assert(is_trivially_copyable<T>::type, "Unsupported type");. This is not what...

Explicit member specialization using IBM Rational Rhapsody

c++,template-specialization,rhapsody
I want to explicitly specialize some member functions inside a class using IBM Rational Rhapsody. What I have done so far; I created a function inside a regular class, marked it as a template. Marked CG::Generate to Specification. This will be the prototype of the template. Then I created another...

How to specialize a template with template-tempate parameters

c++,template-specialization,template-templates
Edit at the end I have a function which takes a template: template <template <typename ...> class P, typename ... Args> void f(const P<Args...> &p) { std::cout << "Template with " << sizeof...(Args) << " parameters!\n"; } It works pretty good with any kind of templates I've tested so far:...

How to partially specialize a template function for a specific template with int paramater

c++,class,templates,c++11,template-specialization
I can't find another question which seems to answer this. I have a template defined as follows: template <int N> class classA; //the body of class A is irrelevant for this problem really. now I have a second class template: template<typename T> class classX { public: static const unsigned number;...