By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

template function with <char*>

P: n/a
I have:

char* byteN = "BYTE";
char* byte_fun()
{
return type_fun< byteN>();
}

char* fix_fun()
{
return type_fun< "FIX">();
}
I the first case, the compiler says:
basic_fun.cpp:490: `lib::byteN' is not a valid template argument
basic_fun.cpp:490: it must be the address of an object with external
linkage
basic_fun.cpp:490: no matching function for call to `type_fun()'

but I think it HAS external linkage, hasn't it?

In the second case the compiler says:
basic_fun.cpp:494: string literal "FIX" is not a valid template argument
because it is the address of an object with static linkage

So why this has to be?

Any suggestions how to do it?
thanks,
marc

Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Marc Schellens" <m_*********@hotmail.com> wrote...
I have:

char* byteN = "BYTE";
It's better not to initialise a pointer to non-const char with
a string literal. While it's allowed for backward compatibility
reasons (the worst reasons ever), you should avoid those. Use

char byteN[] = "BYTE";

or

const char* byteN = "BYTE";
char* byte_fun()
{
return type_fun< byteN>();
What's "type_fun"?
}

char* fix_fun()
{
return type_fun< "FIX">();
}
I the first case, the compiler says:
basic_fun.cpp:490: `lib::byteN' is not a valid template argument
basic_fun.cpp:490: it must be the address of an object with external
linkage
basic_fun.cpp:490: no matching function for call to `type_fun()'

but I think it HAS external linkage, hasn't it?
That's not the point. It has to be an address (constant thing),
and you're passing a pointer, the value of which can change during
run-time.
In the second case the compiler says:
basic_fun.cpp:494: string literal "FIX" is not a valid template argument
because it is the address of an object with static linkage
[String] literals have no linkage.
So why this has to be?
Why what has to be?
Any suggestions how to do it?


How to do what? Post the definition of the 'type_fun' template,
otherwise I have to speculate:

template<char*> char* type_fun() {}

char somechar;
char *foo()
{
return type_fun<&somechar>();
}

What are you trying to accomplish, anyway?

Victor
Jul 19 '05 #2

P: n/a


John Harrison wrote:
"Marc Schellens" <m_*********@hotmail.com> wrote in message
news:3F**************@hotmail.com...
I have:

char* byteN = "BYTE";

extern const char byteN[] = "BYTE";

and the first example should work. The second never will.

char* byte_fun()
{
return type_fun< byteN>();
}

char* fix_fun()
{
return type_fun< "FIX">();
}


Thanks,
this works.
Also without "extern const".
But it is in fact const. And const alone does not work.
extern alone gives a warning.

Jul 19 '05 #3

P: n/a
> "Marc Schellens" <m_*********@hotmail.com> wrote in message
news:3F**************@hotmail.com...
I have:

char* byteN = "BYTE";

extern const char byteN[] = "BYTE";

and the first example should work. The second never will.

char* byte_fun()
{
return type_fun< byteN>();
}

char* fix_fun()
{
return type_fun< "FIX">();
}

BTW: There is no way tp put the definiton of byteN
inside the function?

Jul 19 '05 #4

P: n/a
"Marc Schellens" <m_*********@hotmail.com> wrote...
Victor Bazarov wrote:
"Marc Schellens" <m_*********@hotmail.com> wrote...
[...]
So why this has to be?
Why what has to be?


Why need template arguments to have external linkage


So that the same values (addresses) created the same template.
Otherwise the compiler will try to generate a different function
and that is not what you want (especially if you happen to have
a static variable in the body).
Any suggestions how to do it?

How to do what? Post the definition of the 'type_fun' template,
otherwise I have to speculate:


How to pass the template argument.
template<char*> char* type_fun() {}

char somechar;
char *foo()
{
return type_fun<&somechar>();
}

What are you trying to accomplish, anyway?


Parametrize a function. The function might give out an error.
If it will, I want that it reports which instantiation failed.

template< class TargetClass, DType targetT, const char* funName>
RetType* type_fun();


Create a bunch of global arrays of char, declare them in the same
header, include that header in the header in which you have the
definition of that template function, and use those global arrays
when instantiating the function.

If you're trying to create some kind of debug tool, you might be
better off with a macro, where you could specify a literal...

Victor
Jul 19 '05 #5

P: n/a
"Marc Schellens" <m_*********@hotmail.com> wrote...
"Marc Schellens" <m_*********@hotmail.com> wrote in message
news:3F**************@hotmail.com...
I have:

char* byteN = "BYTE";

extern const char byteN[] = "BYTE";

and the first example should work. The second never will.

char* byte_fun()
{
return type_fun< byteN>();
}

char* fix_fun()
{
return type_fun< "FIX">();
}

BTW: There is no way tp put the definiton of byteN
inside the function?


It won't have linkage if you try that. To have linkage it
has to be declared/defined in a namespace scope.

Victor
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.