tags:

views:

86

answers:

3

So in my .h file I have

template <class T>
void getValue(T *val, int element, int index);

and then in my .cc file I have a function:

template <class T>
void RParser::getValue(T *val, int element, int index)
{

I also have it explicitly instantiated:

    template void RParser::getValue<char>(char *val, int element, std::string subrecName);
    template void RParser::getValue<long long>(long long *val, int element, std::string subrecName);
    template void RParser::getValue<float>(float *val, int element, std::string subrecName);
...

this works but I would like to make a whole different function for std::string

I tried:

template <class std::string>
void RParser::getValue<std::string>(std::string * val, int element, int index)
{

but that didn't work.

Any suggestions would be greatly appreciated,

Thanks, Josh

+4  A: 

Don't use a template at all - just an ordinary function:

void RParser::getValue(std::string * val, int element, int index)
{
}
anon
+2  A: 

If you want to specialise the template, then the syntax is:

template<> 
void RParser::getValue<std::string>(std::string* val, int element, int index) {}

But as Neil's answer says, you don't need to specialise this function template; an overload will do the job:

void RParser::getValue(std::string* val, int element, int index) {}
Mike Seymour
+1  A: 

It's simply illegal C++ to specialize a template member function of a class. I think in C++00x this has been relaxed.emphasized text

EDIT:

class foo {
    template <typename T> T bar(const T & t){
        return t;
    }

    template <> int bar<int>(const int & t){ 
        return t + 1 ;
    }
};


int main(int c, const char * v[]){
    foo f;
}

when compiling

 % gcc /tmp/foo.cpp
/tmp/foo.cpp:6: error: explicit specialization in non-namespace scope ‘class foo’
/tmp/foo.cpp:6: error: template-id ‘bar<int>’ in declaration of primary template
bradgonesurfing
I thought it was only illegal within templated classes?
jon hanson
_Partial_ specialization is illegal. @ jon hanson: That would be inside a _class template_, not inside a _class_. Different animals.
MSalters
specialization of a member function is illegal inside a class or class template. See the edit above with gcc error output.
bradgonesurfing