 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ryjek Guest
|
Posted: Wed Dec 24, 2003 10:50 pm Post subject: Type conversions of string literals |
|
|
The following piece of code compiles on some compilers
(VC++6.0,Intel,SGI's CC,Comeau), but fails on others (gcc,HP's aCC). Is
this code valid ?
struct Text {
Text( const char (&str)[2] ) {}
};
void f( Text a, ... ) {}
int main()
{
Text a("a"); // ok
f("a"); // gcc error: conversion from "const char *"
// to non-scalar type "Text"
return 0;
}
Any idea how to work around the compilation error ? I would like to
allow only string literals to be passed as the first argument to f().
TIA
Ryjek
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Fri Dec 26, 2003 12:55 pm Post subject: Re: Type conversions of string literals |
|
|
"Ryjek" <ryjek (AT) cox (DOT) net> wrote
| Quote: | Any idea how to work around the compilation error ? I would like to
allow only string literals to be passed as the first argument to f().
|
There's no good way to do that. A string literals not easily distinguishable
from a const char array. Even if you got around your compile bugs above
it wouldn't do what you say.
You could use a little syntactic fudge:
#define f(x) realf("" x "")'
void realf(const char*);
f("foo"); // calls realf("foo");
const char a[2];
f(a); // syntax error !
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|