template-specialization

Template struct with the default template argument is not instantiated

江枫思渺然 提交于 2020-02-02 05:10:07
问题 Let's say I have this code template<typename T2, typename T = int> struct X { static double f; }; template<typename T> double X<T>::f = 14.0; If I try to compile clang give me the following error nested name specifier 'X::' for declaration does not refer into a class, class template or class template partial specialization and for GCC : error: template definition of non-template 'double X::f' The question is : Why the compiler want us to specialize the struct X like that : template<typename

Template struct with the default template argument is not instantiated

一笑奈何 提交于 2020-02-02 05:08:54
问题 Let's say I have this code template<typename T2, typename T = int> struct X { static double f; }; template<typename T> double X<T>::f = 14.0; If I try to compile clang give me the following error nested name specifier 'X::' for declaration does not refer into a class, class template or class template partial specialization and for GCC : error: template definition of non-template 'double X::f' The question is : Why the compiler want us to specialize the struct X like that : template<typename

Template struct with the default template argument is not instantiated

回眸只為那壹抹淺笑 提交于 2020-02-02 05:08:52
问题 Let's say I have this code template<typename T2, typename T = int> struct X { static double f; }; template<typename T> double X<T>::f = 14.0; If I try to compile clang give me the following error nested name specifier 'X::' for declaration does not refer into a class, class template or class template partial specialization and for GCC : error: template definition of non-template 'double X::f' The question is : Why the compiler want us to specialize the struct X like that : template<typename

C++ constructor template specialization

旧巷老猫 提交于 2020-02-02 02:59:39
问题 I'm trying to create a specialized constructor for std::string arguments, but the other one is always used when I call it with a string argument. struct Literal : Expression { template <typename V> Literal(V val) { value = val; } }; template <> Literal::Literal(std::string const& val) { value = val.c_str(); } It doesn't matter if both are defined inside the class, both outside the class, or like in the posted example only the specialization is defined outside the class: When called with std:

c++ how does a class derived from a template call the template's constructor?

左心房为你撑大大i 提交于 2020-01-25 12:47:48
问题 I didn't really know how to call this thread. The situation is the following. I have a template class Array<T> : template <typename T> class Array{ private: T* m_cData; int m_nSize; static int s_iDefualtSize; public: Array(); Array(int nSize); } Now, I would like to write a specialized class derived from Array<T> , holding objects of class Boxes : class Boxes{ private: double m_dFull; public: Boxes(double dFull=0): m_dFull(dFull) {} }; I do that in the following manner: class BoxesArray :

template pass by const reference

孤街浪徒 提交于 2020-01-25 06:37:54
问题 I've looked over a few similar questions, but I'm still confused. I'm trying to figure out how to explicitly (not by compiler optimization etc) and C++03-compatible avoid copying of an object when passing it to a specialized template function. Here is my test code: #include <iostream> using namespace std; struct C { C() { cout << "C()" << endl; } C(const C&) { cout << "C(C)" << endl; } ~C() { cout << "~C()" << endl; } }; template<class T> void f(T) { cout << "f<T>" << endl; } // This shows

Problems specializing variable template function

早过忘川 提交于 2020-01-24 09:21:26
问题 I am writing a function inListi() which takes at least one argument and compares the first argument to thes list of all subsequent arguments. returns true if first argument == an element in the list, otherwise false. So: if( inListi( 1.2, 2.3, 4.5, 1.2)) std::cout << "Returns true because last argument equals the first argument." << endl; if( inListi( "hello", "world", "HEllo")) std::cout << "This should print out because of the last argument." << endl; Problem is, it doesn't work. I have the

Adding specializations of template functions later

做~自己de王妃 提交于 2020-01-23 11:37:13
问题 Suppose I have functions like: template<typename T> inline typename std::enable_if<has_member_foo<T>::value,int>::type foo( T const &t ) { return t.foo(); } template<typename T> inline typename std::enable_if<!has_member_foo<T>::value,int>::type foo( T const& ) { return 0; } template<typename T> inline int call_foo( T const &t ) { return sizeof( T ) + foo( t ); } This mostly works fine, but if I later add an overload for a particular type: inline int foo( std::string const &s ) { return s

Is it legal to partially specialise variadic template inner class with args from variadic template of an outer class

僤鯓⒐⒋嵵緔 提交于 2020-01-23 04:34:07
问题 Consider the code: #include <iostream> template <class... Ts> struct outer { template <class... ITs> struct inner { static constexpr bool value = false; }; template <class... ITs> struct inner<Ts..., ITs...> { static constexpr bool value = true; }; }; int main() { std::cout << outer<int, float, double>::inner<int, float, double, int>::value << std::endl; } The code compiles with clang++ but not with g++ where it produces an error: temp3.cc:11:11: error: parameter pack argument ‘Ts ...’ must

Why do templates specialisations need to be inlined?

蹲街弑〆低调 提交于 2020-01-21 01:34:52
问题 I am referring to this answer: https://stackoverflow.com/a/4447057/930315 I ran into a similar issue as the OP of the cited question, having a function template<typename T> void func(T& val); and its specialization template<> void func<mytype>(mytype& val); resulted in a duplicate symbols linker error (the methods are implemented in a '.tpp' file that is included at the end of my header). adding inline to the specialised function resolved the issue. Why? 回答1: According to clause 3.2:4 in the