specialization

friend declaration declares a non-template function [duplicate]

ⅰ亾dé卋堺 提交于 2019-11-26 15:43:32
问题 This question already has an answer here: overloading friend operator<< for template class 5 answers I have a base Class akin to the code below. I'm attempting to overload << to use with cout. However, g++ is saying: base.h:24: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, Base<T>*)’ declares a non-template function base.h:24: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)

explicit specialization of template class member function

痴心易碎 提交于 2019-11-26 15:04:20
I need to specialize template member function for some type (let's say double ). It works fine while class X itself is not a template class, but when I make it template GCC starts giving compile-time errors. #include <iostream> #include <cmath> template <class C> class X { public: template <class T> void get_as(); }; template <class C> void X<C>::get_as<double>() { } int main() { X<int> x; x.get_as(); } here is the error message source.cpp:11:27: error: template-id 'get_as<double>' in declaration of primary template source.cpp:11:6: error: prototype for 'void X<C>::get_as()' does not match any

Template specialization based on inherit class

牧云@^-^@ 提交于 2019-11-26 12:59:32
问题 I want to make this specialized w/o changing main. Is it possible to specialize something based on its base class? I hope so. -edit- I\'ll have several classes that inherit from SomeTag. I don\'t want to write the same specialization for each of them. class SomeTag {}; class InheritSomeTag : public SomeTag {}; template <class T, class Tag=T> struct MyClass { }; template <class T> struct MyClass<T, SomeTag> { typedef int isSpecialized; }; int main() { MyClass<SomeTag>::isSpecialized test1; /

Template specialization of a single method from a templated class

≯℡__Kan透↙ 提交于 2019-11-26 11:59:56
问题 Always considering that the following header, containing my templated class, is included in at least two .CPP files, this code compiles correctly: template <class T> class TClass { public: void doSomething(std::vector<T> * v); }; template <class T> void TClass<T>::doSomething(std::vector<T> * v) { // Do something with a vector of a generic T } template <> inline void TClass<int>::doSomething(std::vector<int> * v) { // Do something with a vector of int\'s } But note the inline in the

Wrong specialized generic function gets called in Swift 3 from an indirect call

眉间皱痕 提交于 2019-11-26 11:38:28
问题 I have code that follows the general design of: protocol DispatchType {} class DispatchType1: DispatchType {} class DispatchType2: DispatchType {} func doBar<D:DispatchType>(value:D) { print(\"general function called\") } func doBar(value:DispatchType1) { print(\"DispatchType1 called\") } func doBar(value:DispatchType2) { print(\"DispatchType2 called\") } where in reality DispatchType is actually a backend storage. The doBar functions are optimized methods that depend on the correct storage

Selecting a member function using different enable_if conditions

帅比萌擦擦* 提交于 2019-11-26 08:56:24
问题 I am trying to determine which version of a member function gets called based on the class template parameter. I have tried this: #include <iostream> #include <type_traits> template<typename T> struct Point { void MyFunction(typename std::enable_if<std::is_same<T, int>::value, T >::type* = 0) { std::cout << \"T is int.\" << std::endl; } void MyFunction(typename std::enable_if<!std::is_same<T, int>::value, float >::type* = 0) { std::cout << \"T is not int.\" << std::endl; } }; int main() {

Template specialization of particular members?

帅比萌擦擦* 提交于 2019-11-26 05:27:21
问题 Is it possible to specialize particular members of a template class? Something like: template <typename T,bool B> struct X { void Specialized(); }; template <typename T> void X<T,true>::Specialized() { ... } template <typename T> void X<T,false>::Specialized() { ... } Ofcourse, this code isn\'t valid. 回答1: You can only specialize it explicitly by providing all template arguments. No partial specialization for member functions of class templates is allowed. template <typename T,bool B> struct

c++ template partial specialization member function [duplicate]

余生颓废 提交于 2019-11-26 04:48:10
问题 This question already has an answer here: “invalid use of incomplete type” error with partial template specialization 3 answers I\'m new to templates so maybe this is a trivial thing but I cannot get it to work. I\'m trying to get partial specialization of a class member function. The shortest code would be: template <typename T, int nValue> class Object{ private: T m_t; Object(); public: Object(T t): m_t(t) {} T Get() { return m_t; } Object& Deform(){ m_t*=nValue; return *this; } }; template

explicit specialization of template class member function

孤街醉人 提交于 2019-11-26 03:49:23
问题 I need to specialize template member function for some type (let\'s say double ). It works fine while class X itself is not a template class, but when I make it template GCC starts giving compile-time errors. #include <iostream> #include <cmath> template <class C> class X { public: template <class T> void get_as(); }; template <class C> void X<C>::get_as<double>() { } int main() { X<int> x; x.get_as(); } here is the error message source.cpp:11:27: error: template-id \'get_as<double>\' in