473,322 Members | 1,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

template function with <char*>

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
5 4436
"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


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
> "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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chris Thompson | last post by:
Hi I'm writing a p2p client for an existing protocol. I used a std::vector<char> as a buffer for messages read from the server. The message length is the first 4 bytes. The message code the...
8
by: Earl Purple | last post by:
On VC++.NET it is implemented like this static int __cdecl compare ( const _Elem *_First1, const _Elem *_First2, size_t _Count ) { // compare [_First1, _First1 + _Count) with [_First2, ...)...
13
by: Richard | last post by:
vector<char*> m_Text; m_Text.resize(1); char* foo = "FOO"; char* bar = "BAR"; char* foobar = (char*)malloc(strlen(foo) + strlen(bar) + 1); if (foobar) { strcpy(foobar, foo); strcat(foobar,...
3
by: silverburgh.meryl | last post by:
I have this linker error, and I would need some help in resolving it: I have put "#include <iostream>" in my .cpp. I am not sure why I can't link. It compiles fine. /usr/bin/ld:...
3
by: Alex Vinokur | last post by:
Is using memcpy() with queue<char> safe? ------ C++ code ------ #include <cstring> #include <iostream> #include <queue> using namespace std; int main() {
8
by: Marco Costa | last post by:
Hello all, I wrote a simple ODBC wrapper class that used code like this ( not real code, added types for clarification ): char** type bufs = new char* for( int i = 0 ; i < numberOfColumns ;...
1
by: sharmadeep1980 | last post by:
Hi All, I am facing a very unique problem while compling my project in "Release" build. The project is building in DEBUG mode but giving linking error on Release build. Here is the error:...
4
by: Jim Langston | last post by:
I'm using a function like this: char TextBuffer; jGet_DropDown_Selected_Text( cc.ddSex, TextBuffer); Where the function is filling in the text buffer. I don't have access to the actual...
6
by: JackC | last post by:
Hi, If i have a vector<charmaybe 15mb in size, whats the most efficient way to write out all the elements to an output file? I have tried this: for(int z = 0; z < Output_Buffer.size(); z++)...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.