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 

compile time strings of one char

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





PostPosted: Wed May 17, 2006 2:21 am    Post subject: compile time strings of one char Reply with quote



Hello,

I need to create and get access to the compile time string of 'N'
length
where 'N' is the template class argument of type int.

The string should be of one char only.

Here is my solution:

////////////////////////////////////////////////

template <char ch, int n> struct cx_one_char : public cx_one_char<ch,
n-1> {
static const char _ch = ch; };

template <char ch> struct cx_one_char<ch, 0> {
static const char _ch = '\0'; };

int main() {

const char *p_char1 = &cx_one_char<'A', 4>::_ch; // returns "AAAA"
const char *p_char2 = &cx_one_char<'A', 3>::_ch; // returns "AAA"
const char *p_char3 = &cx_one_char<'A', 5>::_ch; // returns "AAAAA"
const char *p_char4 = &cx_one_char<'A', 10>::_ch;
const char *p_char5 = &cx_one_char<'A', 0>::_ch; // returns ""
const char *p_char6 = &cx_one_char<'A', 1>::_ch; // returns "A"

return 0;
}

////////////////////////////////////////////////

While it works on MS VC 2005 there is no doubt that it relies on
several things
like char alignment of 1, so I guess it is a bit dangerous.

Does more robust solution exist?

Thank you, Dmitry


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Bob Hairgrove
Guest





PostPosted: Sat May 20, 2006 10:21 pm    Post subject: Re: compile time strings of one char Reply with quote



On 16 May 2006 21:48:50 -0400, "dmitry.sychov (AT) mail (DOT) ru"

<dmitry.sychov (AT) mail (DOT) ru> wrote:



Quote:
Hello,



I need to create and get access to the compile time string of 'N'

length

where 'N' is the template class argument of type int.



The string should be of one char only.



Here is my solution:



////////////////////////////////////////////////



template <char ch, int n> struct cx_one_char : public cx_one_char<ch,

n-1> {

static const char _ch = ch; };



template <char ch> struct cx_one_char<ch, 0> {

static const char _ch = '\0'; };



int main() {



const char *p_char1 = &cx_one_char<'A', 4>::_ch; // returns "AAAA"

const char *p_char2 = &cx_one_char<'A', 3>::_ch; // returns "AAA"

const char *p_char3 = &cx_one_char<'A', 5>::_ch; // returns "AAAAA"

const char *p_char4 = &cx_one_char<'A', 10>::_ch;

const char *p_char5 = &cx_one_char<'A', 0>::_ch; // returns ""

const char *p_char6 = &cx_one_char<'A', 1>::_ch; // returns "A"



return 0;

}



////////////////////////////////////////////////



While it works on MS VC 2005 there is no doubt that it relies on

several things

like char alignment of 1, so I guess it is a bit dangerous.



Does more robust solution exist?



I don't think so, not as a template. You would always be relying on

implementation-defined behavior with this, according to the C++

standard, because each compiler can add padding between its members as

it chooses. And it is also implementation-defined where it stores

static objects of a class.



Is it really necessary to have this as a template?



--

Bob Hairgrove

NoSpamPlease (AT) Home (DOT) com



[ 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.