Template template specialization in template class

Refactoring outdated code. I want to combine separate classes / patterns of templates that are related to each other (to avoid pollution of the namespace).

Nested(below) is a helper class for MyStructwhich I want to move to MyStruct .

But I can not do this job:

#include <type_traits>
#include <iostream>

struct YES {} ;
struct NO {};

template <typename TYPE>
struct MyStruct
{

    template <typename TYPE_AGAIN = TYPE, typename SELECTOR = NO>
    struct Nested
    {
        static void Print(void)
        {
            std::cout << "MyStruct::Nested<bool = false>::Print()" << std::endl;
        }
    };


    template <>
    struct Nested<TYPE, typename std::enable_if<std::is_integral<TYPE>::value, YES>::type>
    {
        static void Print(void)
        {
           std::cout << "MyStruct::Nested<bool = true>::Print()" << std::endl;
        }
    }; 

};

the compiler complains:

g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
In file included from ../main.cpp:8:0:
../MyStruct.h:31:12: error: explicit specialization in non-namespace scope ‘struct MyStruct<TYPE>’
  template <>
            ^
make: *** [main.o] Error 1

In fact, it also prevents me from turning on

<typename TYPE_AGAIN = TYPE>

But without, there are even more complaints:

g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
In file included from ../main.cpp:8:0:
../MyStruct.h:31:12: error: explicit specialization in non-namespace scope ‘struct MyStruct<TYPE>’
  template <>
            ^
../MyStruct.h:32:9: error: template parameters not used in partial specialization:
  struct Nested<typename std::enable_if<std::is_integral<TYPE>::value, YES>::type>
     ^
../MyStruct.h:32:9: error:         ‘TYPE’
make: *** [main.o] Error 1
+4
source share
3 answers

You cannot specialize templates in fields without a namespace, for example structin your case.

:

template<typename TYPE> template<>
struct MyStruct<TYPE>::Nested<...> {};

, , . -, .

, :

template<> template<>
struct MyStruct<int>::Nested<...> {};

, SFINAE:

template<typename SELECTOR>
struct Nested; // Default invalid SELECTOR

template<>
struct Nested<YES> { /*...*/ };

template<>
struct Nested<NO> { /*...*/ };
+5

, , :

, :

 #include <type_traits>
 #include <iostream>
 #include <type_traits>

 //Crédit: cpp-reference.com
 template<typename... Ts> struct make_void { typedef void type;};
 template<typename... Ts> using void_t = typename make_void<Ts...>::type;

 template <typename TYPE>
 struct MyStruct
 {
     //The third parameter is an unused parameter 
     template <typename TYPE_AGAIN = TYPE, typename SELECTOR = void
               , typename = void>
     struct Nested
     {
         static void Print(void)
         {
             std::cout << "MyStruct::Nested<bool = false>::Print()" << std::endl;
         }
     };

     //We declare a partial specialization, T go to the third parameter. We use the void_t trick here.
     template <class T>
     struct Nested<TYPE,
                  void_t<typename std::enable_if<
                                      std::is_integral<TYPE>::value>::type>
                        ,T>
     {
         static void Print(void)
         {
            std::cout << "MyStruct::Nested<bool = true>::Print()" << std::endl;
         }
     }; 

 };

 int main(){
    MyStruct<int>::Nested<>::Print();
    return EXIT_SUCCESS;
 }
+1

:

#include <type_traits>
#include <iostream>

template <typename TYPE>
struct MyStruct
{
  template <typename SEL_TYPE = TYPE>
  static typename std::enable_if<std::is_integral<SEL_TYPE>::value && std::is_same<TYPE, SEL_TYPE>::value, void>::type
  Print(void)
  {
    std::cout << "MyStruct::Nested<is_integral::Print()" << std::endl;
  }

  template <typename SEL_TYPE = TYPE>
  static typename std::enable_if<!std::is_integral<SEL_TYPE>::value && std::is_same<TYPE, SEL_TYPE>::value, void>::type
  Print(void)
  {
    std::cout << "MyStruct::Nested<not is_integral>::Print()" << std::endl;
  }

};

- .

Print(), .

std:: is_same < > , SEL_TYPE = TYPE > . -

MyStruct<std::string>::Print<int>();
0

Source: https://habr.com/ru/post/1657898/


All Articles