See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

Accessing class template members from static functions of a specialized template of the same class

General Tech Learning Aids/Tools

Max. 2000 characters



( 5 months ago )


I am still learning C++ templates, and have encountered a problem regarding calling members from specialized static functions using the following. GCC complains: "invalid use of member C< const char* >::value in static member function." I have searched this forum and a few others, and even my friend Google cannot aid me. I figure the error has to be something I am overlooking, as I made a non-specialized version of the class (with the same static member function), and I still get the same error. Any ideas?

template <typename T = const char*>
class C { };

//specialization for const char*
template <>
class C <const char*> {
    C() { }

    static void echo(int x);

    int value;

//error occurs here
void C<const char*>::echo(int x) {
  value = x;

Many thanks to any insight you can offer.



( 5 months ago )

It has nothing to do with templates.

value is an instance member, and can only be accessed when you provide an instance of C. A static function has no this instance, and you haven't used the . or -> member access operator either to explicitly provide an instance.

what's your interest