C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Type conversions of string literals

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Ryjek
Guest





PostPosted: Wed Dec 24, 2003 10:50 pm    Post subject: Type conversions of string literals Reply with quote



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





PostPosted: Fri Dec 26, 2003 12:55 pm    Post subject: Re: Type conversions of string literals Reply with quote




"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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.