template-specialization

template class with a single method specialized in C++

99封情书 提交于 2020-12-05 07:12:07
问题 I only have an hpp file for a school assignment in C++ (I am not allowed to add a cpp file, declaration and implementation should be both written in the file). I wrote this code inside it: template<class T> class Matrix { void foo() { //do something for a T variable. } }; I would like to add another foo method, but this foo() will be specialized for only an <int> . I have read in some places that I need to declare a new specialization class for this to work. But what I want is that the

template class with a single method specialized in C++

╄→гoц情女王★ 提交于 2020-12-05 07:11:59
问题 I only have an hpp file for a school assignment in C++ (I am not allowed to add a cpp file, declaration and implementation should be both written in the file). I wrote this code inside it: template<class T> class Matrix { void foo() { //do something for a T variable. } }; I would like to add another foo method, but this foo() will be specialized for only an <int> . I have read in some places that I need to declare a new specialization class for this to work. But what I want is that the

Is it safe to place definition of specialization of template member function (withOUT default body) in source file?

与世无争的帅哥 提交于 2020-05-28 13:48:02
问题 Here's what I mean: // test.h class cls { public: template< typename T > void f( T t ); }; - // test.cpp template<> void cls::f( const char* ) { } - // main.cpp int main() { cls c; double x = .0; c.f( x ); // gives EXPECTED undefined reference (linker error) const char* asd = "ads"; c.f( asd ); // works as expected, NO errors return 0; } This is completely fine, right? I started doubting this, because I just ran over the specialization of '...' after instantiation error, which was new to me.

Is it safe to place definition of specialization of template member function (withOUT default body) in source file?

好久不见. 提交于 2020-05-28 13:47:59
问题 Here's what I mean: // test.h class cls { public: template< typename T > void f( T t ); }; - // test.cpp template<> void cls::f( const char* ) { } - // main.cpp int main() { cls c; double x = .0; c.f( x ); // gives EXPECTED undefined reference (linker error) const char* asd = "ads"; c.f( asd ); // works as expected, NO errors return 0; } This is completely fine, right? I started doubting this, because I just ran over the specialization of '...' after instantiation error, which was new to me.

Module dependencies when using template function specializations

血红的双手。 提交于 2020-05-28 09:23:32
问题 Please check out the following code snippet (pseudo code, didn't compile it): A.h template <typename T> void SubTest(T t) = delete; template <> void SubTest(int i) { cout << i; } template <typename T> class MyClass { public: void Test(T t) { SubTest(t); } } B.h class X{}; template <> void SubTest(X x) { cout << x; } As you can see I want a class template method to call a function template. That function is specialized in various ways. Some of the specializations are located in A.h, some in

Inlining Template Specialization

烂漫一生 提交于 2020-05-27 02:18:33
问题 If I have a header foo.h which I include all over my project, it seems to work fine when all it contains is: template<typename T> void foo(const T param) { cout << param << endl; } But I get one definition rule (ODR) errors when I add a specalization to foo.h: template<> void foo(const bool param) { cout << param << endl; } Obviously I can solve this by inline 'ing the specialization. My question is, why do I need to? If the template doesn't violate ODR, why does a specialization? 回答1: An

Inlining Template Specialization

↘锁芯ラ 提交于 2020-05-27 02:16:00
问题 If I have a header foo.h which I include all over my project, it seems to work fine when all it contains is: template<typename T> void foo(const T param) { cout << param << endl; } But I get one definition rule (ODR) errors when I add a specalization to foo.h: template<> void foo(const bool param) { cout << param << endl; } Obviously I can solve this by inline 'ing the specialization. My question is, why do I need to? If the template doesn't violate ODR, why does a specialization? 回答1: An

Inlining Template Specialization

◇◆丶佛笑我妖孽 提交于 2020-05-27 02:14:58
问题 If I have a header foo.h which I include all over my project, it seems to work fine when all it contains is: template<typename T> void foo(const T param) { cout << param << endl; } But I get one definition rule (ODR) errors when I add a specalization to foo.h: template<> void foo(const bool param) { cout << param << endl; } Obviously I can solve this by inline 'ing the specialization. My question is, why do I need to? If the template doesn't violate ODR, why does a specialization? 回答1: An