class template state data member, not an entity that can be explicitly specialized
Asked Answered
G

1

13

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?

Glacialist answered 6/1, 2013 at 1:41 Comment(7)
Leave out the template<> in the definition of value.Coppinger
@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.Linares
@jaredC Should the OP delete his question?Gloria
@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...Linares
@Linares it's usually up to the answerer and asker.Bronder
@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.Coppinger
@KerrekSB Thanks, that makes a lot of sense, particularly the fleshed out and educational answer part.Linares
K
14

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";
Kanzu answered 8/1, 2013 at 19:11 Comment(3)
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.Hammerfest
@JohannesSchaub-litb Yes, good points, I wasn't too concerned with that as the poster didn't have a main() etc.Kanzu
For clarity for noobs like me: what's happening is that OP was trying to explicitly specialize the class (class_name<string, false>), and then explicitly specialize that class' member. Either specialize the class, or specialize the member.Tolerant

© 2022 - 2024 — McMap. All rights reserved.