template function with <char*>

M

Marc Schellens

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
 
V

Victor Bazarov

Marc Schellens said:
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
 
M

Marc Schellens

John said:
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.
 
M

Marc Schellens

Marc Schellens said:
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?
 
V

Victor Bazarov

Marc Schellens said:
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).
How to pass the template argument.


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
 
V

Victor Bazarov

Marc Schellens said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top