5

As the title says I want to specialize a function template for both string and char pointer, so far I did this but I can not figure out passing the string parameters by reference.

#include <iostream>
#include <string.h>
template<typename T> void xxx(T param)
{
std::cout << "General : "<< sizeof(T)  << std::endl;
}

template<> void xxx<char*>(char* param)
{
std::cout << "Char ptr: "<< strlen(param) << std::endl;
}

template<> void xxx<const char* >(const char*  param)
{
std::cout << "Const Char ptr : "<< strlen(param)<<  std::endl;
}

template<> void xxx<const std::string & >(const std::string & param)
{
std::cout << "Const String : "<< param.size()<<  std::endl;
}

template<> void xxx<std::string >(std::string param)
{
std::cout << "String : "<< param.size()<<  std::endl;
}


int main()
{
        xxx("word");
        std::string aword("word");
        xxx(aword);

        std::string const cword("const word");
        xxx(cword);
} 

Also template<> void xxx<const std::string & >(const std::string & param) thing just does not working.

If I rearranged the opriginal template to accept parameters as T& then the char * is required to be char * & which is not good for static text in code.

Please help !

1
  • 1
    Now it doesn't compile! You need to put <string.h> back, for strlen. Commented Jan 12, 2011 at 16:39

3 Answers 3

11

Doesn’t the following work?

template<>
void xxx<std::string>(std::string& param)
{
    std::cout << "String : "<< param.size()<<  std::endl;
}

And the same for const std::string?

That said, don’t specialize a function template if you have the choice (and you usually do!). Instead, just overload the function:

void xxx(std::string& param)
{
    std::cout << "String : "<< param.size()<<  std::endl;
}

Notice, this is not a template. In 99% of the cases, this is fine.

(Something else, C++ doesn’t have a header <string.h> except for backwards compatibility to C. The C-string header in C++ is called <cstring> (note the leading c) but from your code it look as though you actually mean the header <string> (no leading c).)

Sign up to request clarification or add additional context in comments.

5 Comments

Ah sorry for the <string.h>. I will fix it.
A link would be nice as to why it shouldn't be specialized, so here it is: gotw.ca/publications/mill17.htm
@stefaanv: nice, will include.
@sad_man: no, it's in Konrads answer and to be honest, I wasn't aware of this before...
Have you compiled this? g++ 4.4.0 doesn't accept it. It gives the error message: error: template-id 'xxx<std::string>' for 'void xxx(std::string&)' does not match any template declaration
0

Here is a distillation of what I find surprising:

#include <iostream>

template<typename T> void f(T param) { std::cout << "General" << std::endl ; }
template<> void f(int& param) { std::cout << "int&" << std::endl ; }

int main() {
  float x ; f (x) ;
  int y ; f (y) ;
  int& z = y ; f (z) ;
}

This prints "General" 3 times. The first time (float) is expected, the third time (int&) is a surprise. Why doesn't this work?

1 Comment

This is the problem I have with std::string&. There is something wrong with passing referenced types in a template. I wish somebody explains it.
0

is really risky try to coding using compiler-based-type-conversions

one thing is the use of a template based, and other is the use of polymorph with different types.

depend of the compiler you can get different behaviors.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.