12

I got an error in the code below:

template<typename T, bool B = is_fundamental<T>::value>
class class_name;

template<>
class class_name<string, false>{
public:
    static string const value;
};

template<>
string const class_name<string, false>::value = "Str";
// error: not an entity that can be explicitly specialized.(in VC++)

How can I fix it?

TemplateRex
  • 69,038
  • 19
  • 164
  • 304
  • 9
    Leave out the `template<>` in the definition of `value`. – Kerrek SB Jan 06 '13 at 01:45
  • @KerrekSB Would an answer like this typically never become "answered"? I'm trying to learn what we should/shouldn't do when we answer something in a comment. – JaredC Jan 06 '13 at 02:15
  • @jaredC Should the OP delete his question? – David G Jan 06 '13 at 02:57
  • @David I'm actually wondering if Kerrek should make an official answer that the OP accepts once the comments have determined that its the correct answer. Just wondering what the usual protocol is... – JaredC Jan 06 '13 at 02:59
  • @JaredC it's usually up to the answerer and asker. – Seth Carnegie Jan 06 '13 at 03:04
  • @JaredC: There are several options. First off, I sometimes feel too not-bothered or tired to flesh out a complete answer, so I just leave a comment. I feel that a complete answer should probably explain why the definition isn't a specialization, etc., and I didn't want to go and look that up. The OP could delete the question if it is no longer relevant (and relevant to *others*!), or he could edit it to ask explicitly for a detailed explanation, in which case someone else may very well go and write up an educational answer. – Kerrek SB Jan 06 '13 at 12:23
  • @KerrekSB Thanks, that makes a lot of sense, particularly the fleshed out and educational answer part. – JaredC Jan 06 '13 at 14:48

1 Answers1

13

You are mixing two different approaches here. The first is the one suggested by @KerrekSB

template<typename T, bool B = is_fundamental<T>::value>
class class_name;

// NOTE: template<> is needed here because this is an explicit specialization of a class template
template<>
class class_name<string, false>{
public:
    static string const value;
};

// NOTE: no template<> here, because this is just a definition of an ordinary class member 
// (i.e. of the class class_name<string, false>)
string const class_name<string, false>::value = "Str";

Alternatively, you could full write out the general class template and explicitly specialize the static member for <string, false>

template<typename T, bool B = is_fundamental<T>::value>
class class_name {
public:
    static string const value;
};

// NOTE: template<> is needed here because this is an explicit specialization of a class template member
template<>
string const class_name<string, false>::value = "Str";
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
  • Upvoted. Also, in both cases the definitions need to be in the CPP file and for the specialization-of-member case, you need to declare the specialization in the header. – Johannes Schaub - litb Jan 08 '13 at 19:15
  • @JohannesSchaub-litb Yes, good points, I wasn't too concerned with that as the poster didn't have a `main()` etc. – TemplateRex Jan 08 '13 at 19:17
  • 1
    For clarity for noobs like me: what's happening is that OP was trying to explicitly specialize the class (`class_name`), and then explicitly specialize that class' member. Either specialize the class, or specialize the member. – Kyle Jul 03 '19 at 14:46