template-specialization

Class template specialization priority/ambiguity

ε祈祈猫儿з 提交于 2019-12-04 16:20:25
问题 While trying to implement a few things relying on variadic templates, I stumbled accross something I cannot explain. I boiled down the problem to the following code snippet: template <typename ... Args> struct A {}; template <template <typename...> class Z, typename T> struct test; template <template <typename...> class Z, typename T> struct test<Z, Z<T>> { static void foo() { std::cout << "I'm more specialized than the variadic spec, hehe!" << std::endl; } }; template <template <typename...>

Multiple SFINAE class template specialisations using void_t

笑着哭i 提交于 2019-12-04 15:39:13
问题 Are multiple class template specialisations valid, when each is distinct only between patterns involving template parameters in non-deduced contexts? A common example of std::void_t uses it to define a trait which reveals whether a type has a member typedef called "type". Here, a single specialisation is employed. This could be extended to identify say whether a type has either a member typedef called "type1", or one called "type2". The C++1z code below compiles with GCC, but not Clang. Is it

Is it possible to specialize a template definition based on the existence of a nested typedef of a template type parameter?

风格不统一 提交于 2019-12-04 12:35:07
问题 I have a template, template <typename T> class wrapper , that I would like to specialize based on the existence of typename T::context_type . If typename T::context_type is declared, then the constructors and assignment operator overloads of the wrapper<T> instantiation should accept a mandatory typename T::context_type parameter. Additionally, wrapper<T> objects would store "context" in the member data. If typename T::context_type does not exist, then the constructors and assignment operator

Enforce template type through static_assert

橙三吉。 提交于 2019-12-04 10:07:44
问题 I'm trying to understand the usefulness of static_assert , and I want to know if it can help me in enforcing a design, and if so, how. I have a general template class that hides its own implementation inside another template class which is partially specialized based on the size of the template type. Here's a brief outline of this design: template <class T, size_t S = sizeof(T)> struct Helper; template <class T> struct Helper<T, sizeof(long)> { static T bar(); }; // ... other specializations

Partial specilization of static variable template in class template

我的梦境 提交于 2019-12-04 09:10:26
If I do partial specialization I got different results from clang and g++. template < typename T> class X { public: T i; X(T _i): i{_i}{} operator T(){ return i; } }; template < typename T2 > class Y { public: template <typename T> static X<T> x_in_y; }; template< typename T2> template< typename T> X<T> Y<T2>::x_in_y{200}; template<> template<> X<float> Y<int>::x_in_y<float>{100}; template<> template<> X<int> Y<int>::x_in_y<int>{101}; template< > template< typename T > X<T> Y<bool>::x_in_y{77}; int main() { std::cout << Y<int>::x_in_y<int> << std::endl; std::cout << Y<int>::x_in_y<float> <<

Template specialization for an empty parameter pack

安稳与你 提交于 2019-12-04 09:10:07
问题 I have a variadic template function which calls itself to determine the largest number in a list (constituted by the templatized arguments). I am trying to make a specialization for when the parameter pack is empty so I can just return the number at the front of the list, but I don't know how to do that. I am just becoming familiar with variadic templates and template specialization, but this is what I have so far: #include <string> #include <iostream> using namespace std; template <int N,

Conditional enable an alternative assignment operator

烈酒焚心 提交于 2019-12-04 07:35:33
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< > struct StrangerTypeRules < bool > { typedef X<bool> type; }; template<typename T> struct X { // In the

c++ How to initialize static variables of a partial template specialization

前提是你 提交于 2019-12-04 06:09:45
How should I initialize a static variable for a partial specialization? template <bool A=true, bool B=false> struct from { const static std::string value; }; // no specialization - works template <bool A, bool B> const std::string from<A, B>::value = ""; // partial specialization - does not compile - // Error: template argument list following class template name must list parameters in the order used in template parameter list // Error: from<A,B>' : too few template arguments template <bool B> const std::string from<true, B>::value = ""; // full specialization - works const std::string from

Partially specializing member-function implementations

不羁岁月 提交于 2019-12-04 04:41:51
问题 I'm currently refactoring some code the explicitly specializes a member function of a class template with two template parameters. template <class S, class T> class Foo { void bar(); }; template <class S, class T> void Foo<S, T>::bar() { /* Generic stuff */ } template <> void Foo<SomeType, SomeType>::bar() { /* Some special function */ } Now I added some more template parameters, so the class now looks like this: template <class S, class EXTRA0, class T, class EXTRA1> class Foo { void bar();

Is it legal to perform partial in-class specialization of a member template class in derived class

我只是一个虾纸丫 提交于 2019-12-04 04:30:19
It is continuation of this question. I am specifically interested if the partial specialization of a member class like this: struct FooParent { template <class> struct Bar{ }; }; struct Foo: FooParent { template <class T> struct Bar<T*> {}; }; I know this can be done inside a namespace scope: template <class T> struct Foo::Bar<T*>{ }; But I'm also specifically interested in in-class partial specialization at the level of derived class. Both clang and gcc complains when encounter a former: clang states that there is an explicit template specialization which obviously does not occur: error: