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

喜你入骨 提交于 2019-12-06 01:31:13

问题


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<false, true>::value = "";

Why doesn't the partial work?

EDIT: I found a solution based on Partial template specialization for initialization of static data members of template classes

I need to repeat the declaration for the partial specialization before it allowed me to initialize the static variable:

template <bool B>
struct from<true, B> {
    const static std::string value; 
};

Again, the question is why?


回答1:


Partial specialization of members (whether they're functions or static data) are not allowed without partial specialization of enclosing class template itself.

That is, you have to specialize the class template also. So the following should work:

//partial specialization of class template
template <bool B>
struct from<true, B> {
    const static std::string value; 
};

//now you can do this!    
template <bool B>
const std::string from<true, B>::value = ""

Also, this will not compile (have you tried compiling this?):

// full specialization - works (SORRY, IT WILL NOT WORK!)
const std::string from<false, true>::value = "";  //this should be an error

You've to write this:

// full specialization 
template<>   //<---- this is important!
const std::string from<false, true>::value = ""



回答2:


Here's a working full specialization of the template.

#include <string>
#include <iostream> 

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 = "no specialization";

// full specialization, note the empty template parameter list
template <>
const std::string from<true, true>::value = "<true,true> specialization";


int main() {
   std::cout << from<false, false>::value << std::endl;
   std::cout << from<true, true>::value << std::endl;
}

You found the correct way of defining the partial.

The reason for your partial not working is that you need to declare the structure type before being able to provide an initialization for its static field. The partial specialization is a template in its own right, and deserves a definition.

The full specialization is actually a type instance of the initial template, and thus doesn't need to be defined separately.



来源:https://stackoverflow.com/questions/13404695/c-how-to-initialize-static-variables-of-a-partial-template-specialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!