template-specialization

Conditional enable an alternative assignment operator

南笙酒味 提交于 2019-12-21 15:08:07
问题 I'm trying to conditionally instantiate an extra assignment operator. The code below works fine in clang, but not in gcc 4.7. The problem I'm having seems very similar the the question asked here: std::enable_if to conditionally compile a member function The following illustrates the problem I'm having: #include <type_traits> template<typename T> struct StrangerTypeRules; template<typename T> struct X; template< > struct StrangerTypeRules < unsigned > { typedef unsigned type; }; template< >

Conditional enable an alternative assignment operator

夙愿已清 提交于 2019-12-21 15:06:08
问题 I'm trying to conditionally instantiate an extra assignment operator. The code below works fine in clang, but not in gcc 4.7. The problem I'm having seems very similar the the question asked here: std::enable_if to conditionally compile a member function The following illustrates the problem I'm having: #include <type_traits> template<typename T> struct StrangerTypeRules; template<typename T> struct X; template< > struct StrangerTypeRules < unsigned > { typedef unsigned type; }; template< >

Specialization of inherited nested template class

房东的猫 提交于 2019-12-21 12:56:31
问题 The following source code is brought from: Understanding partial specialization of inherited nested class templates #include <type_traits> struct Base { template<class U, class _ = void> struct Inner: std::true_type {}; template<class _> struct Inner<char, _>: std::false_type {}; }; struct Derived : Base { }; template<class _> struct Derived::Inner<int, _>: std::false_type {}; I had an issue about specializing inherited class, so I googled, and find out the question above. The source code in

Ambiguous partial specializations with Clang in C++17

落爺英雄遲暮 提交于 2019-12-21 07:36:25
问题 template <typename Foo, Foo Part> struct TSelect {}; enum What { The }; template <typename Foo> struct AnotherOneSelector { static constexpr Foo Id = Foo::The; }; template <typename Foo, typename SelectPartType> struct THelper; template <typename Foo> struct THelper<Foo, TSelect<Foo, AnotherOneSelector<Foo>::Id>> {}; template <typename Foo, Foo PartId> struct THelper<Foo, TSelect<Foo, PartId>> {}; int main() { THelper<What, TSelect<What, What::The>> t; } This code compiles with gcc8.1 with

Partial template specialization ambiguity

喜欢而已 提交于 2019-12-21 07:15:14
问题 I cant see why the statement in main is ambiguous. template<class T, class U, int I> struct X { void f() { cout << "Primary template" << endl; } }; template<class T, int I> struct X<T, T*, I> {void f() { cout << "Partial specialization 1" << endl;}}; template<class T, class U, int I> struct X<T*, U, I> {void f() { cout << "Partial specialization 2" << endl;}}; template<class T> struct X<int, T*, 10> {void f() { cout << "Partial specialization 3" << endl;}}; template<class T, class U, int I>

c++ function template specialisation

不羁的心 提交于 2019-12-21 04:24:40
问题 Given this code: class X { public: template< typename T > void func( const T & v ); }; template<> void X::func< int >( const int & v ) { } template<> void X::func< char * >( const char * & v ) // 16 { } When I compile it I get the following error. test.cpp:16: error: template-id 'func<char*>' for 'void X::func(const char*&)' does not match any template declaration Can anyone shed any light on this? 回答1: The reason you face this error is because you write const before the type. Although this

function template specialization compile error

一世执手 提交于 2019-12-21 03:33:35
问题 ##A.hh template<class T> void func(T t) {} template<> void func<int>(int t) {} void func2(); ##A.cpp void func2() {} ##main.cpp func("hello"); func(int()); The error I get is: error LNK2005: "void __cdecl func(int)" (??$func@H@@YAXH@Z) already defined in A.obj, one or more multiply defined symbols found Is a function template specialization not treated as a normal function template? It looks like it will be in the objective file for A. 回答1: As template<> void func<int>(int t) {} is a function

Is it possible to access values of non-type template parameters in specialized template class?

十年热恋 提交于 2019-12-20 21:55:34
问题 Is it possible to access values of non-type template parameters in specialized template class? If I have template class with specialization: template <int major, int minor> struct A { void f() { cout << major << endl; } } template <> struct A<4,0> { void f() { cout << ??? << endl; } } I know it the above case it is simple to hardcode values 4 and 0 instead of using variables but what I have a larger class that I'm specializing and I would like to be able to access the values. Is it possible

Role of default template arguments in the context of partial specialization

五迷三道 提交于 2019-12-20 07:35:29
问题 I am not clear about the interaction of default template arguments in the context of partial specialization, for choosing which is the better matching template. This questions stems from code posted in this answer by max66. Given the definitions of the classes A and B : template <int N> struct A { static const int code = N; }; struct B{}; and the following template classes: // primary template template <typename, typename Enable = bool_constant<true>> struct cond : public bool_constant<false>

Partial specialization of member function [duplicate]

会有一股神秘感。 提交于 2019-12-19 19:45:03
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: “invalid use of incomplete type” error with partial template specialization Why is it that I can do this: template <typename T> struct A { void foo(int); }; template <> void A<int>::foo(int) { } but not this: template <typename> struct C {}; template <typename T> struct A { void foo(int); }; template <typename T> void A<C<T> >::foo(int) { } For the second case, GCC gives the following error: test.cpp:10:23: