Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template function with <char*>

Reply
Thread Tools

template function with <char*>

 
 
Marc Schellens
Guest
Posts: n/a
 
      07-31-2003
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-31-2003
"Marc Schellens" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Marc Schellens
Guest
Posts: n/a
 
      07-31-2003


John Harrison wrote:
> "Marc Schellens" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>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.

 
Reply With Quote
 
Marc Schellens
Guest
Posts: n/a
 
      07-31-2003
> "Marc Schellens" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>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?

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-31-2003
"Marc Schellens" <(E-Mail Removed)> wrote...
> Victor Bazarov wrote:
> > "Marc Schellens" <(E-Mail Removed)> 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


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-31-2003
"Marc Schellens" <(E-Mail Removed)> wrote...
> > "Marc Schellens" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> >
> >>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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
Template function as argument to another template function Jim West C++ 3 10-07-2004 07:21 PM
function non-template versus fully specialized function template Robert Allan Schwartz C++ 1 08-09-2004 03:41 PM



Advertisments