Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » C++

How to get string length in compile-time?

 
Jump to:  
 
zade
PostPosted: Fri Sep 05, 2008 4:23 am    Post subject: How to get string length in compile-time?
       
If I know the string content int compile-time, such as "ABC", I think
I can get its length in that time. I have tried the below method to do
that, but it failed:

template<const char* str>
struct GetStringLength
{
static const int value = 1 + GetStringLength<str + 1>::value;
};

template<> struct GetStringLength<NULL>
{
static const int value = 0;
};

any advice, thanks!

--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
red floyd
PostPosted: Fri Sep 05, 2008 9:36 am    Post subject: Re: How to get string length in compile-time?
       
zade wrote:
Quote:
If I know the string content int compile-time, such as "ABC", I think
I can get its length in that time. I have tried the below method to do
that, but it failed:

template<const char* str
struct GetStringLength
{
static const int value = 1 + GetStringLength<str + 1>::value;
};

template<> struct GetStringLength<NULL
{
static const int value = 0;
};

any advice, thanks!


sizeof.

--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Alberto Ganesh Barbati
PostPosted: Fri Sep 05, 2008 9:38 am    Post subject: Re: How to get string length in compile-time?
       
zade ha scritto:
Quote:
If I know the string content int compile-time, such as "ABC", I think
I can get its length in that time. I have tried the below method to do
that, but it failed:

template<const char* str
struct GetStringLength
{
static const int value = 1 + GetStringLength<str + 1>::value;
};

template<> struct GetStringLength<NULL
{
static const int value = 0;
};

It won't work. But I guess you already knew that...

Quote:

any advice, thanks!


template <int N>
int GetStringLength(char const (&a)[N])
{
return N;
}

Be aware that this function counts the terminating null. If you don't
want to count the null, make it return N-1.

HTH,

Ganesh

--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Hakusa@gmail.com
PostPosted: Fri Sep 05, 2008 10:58 am    Post subject: Re: How to get string length in compile-time?
       
On Sep 4, 11:23 pm, zade <zhaohongc...@gmail.com> wrote:
Quote:
template<const char* str
struct GetStringLength
{
static const int value = 1 + GetStringLength<str + 1>::value;

};

template<> struct GetStringLength<NULL
{ ... }

str will NEVER be equal to NULL. Because of this, you'd have an
infinitely recursing type were it not for overflow. I'd try adding a
template parameter: the char that str points to.

I'm curious what might happen if you pass it a value not known at
compile time.


--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Jiang
PostPosted: Fri Sep 05, 2008 5:58 pm    Post subject: Re: How to get string length in compile-time?
       
On Sep 5, 6:38 pm, Alberto Ganesh Barbati <AlbertoBarb...@libero.it>
wrote:
Quote:
zade ha scritto:

[...]

Quote:

template <int N
int GetStringLength(char const (&a)[N])
{
return N;
}

Be aware that this function counts the terminating null. If you don't
want to count the null, make it return N-1.


This function does not work for pointer-to-char-array and
fix size char array.

$ cat length.cpp

#include <stdio.h>

template <int N>
int GetStringLength(char const (&a)[N])
{
return N;
}

const char* str1 = "won't compile" ;
char str2[1024] = "not that long!" ;
const char str3[] = "why not use sizeof?";

#include <stdio.h>

int main()
{
// GetStringLength(str1) , won't compile
printf("string: %s:\t\t length:%d\n", str2,
GetStringLength(str2));
printf("string: %s:\t length:%d\n", str3, GetStringLength(str3));
printf("string: %s:\t sizeof:%d\n", str3, sizeof(str3));
}

$ ./length
string: not that long!: length:1024
string: why not use sizeof?: length:20
string: why not use sizeof?: sizeof:20

So GetStringLength can be used to get the dimension of an array,
but this is not what zade really want in my mind.

For the original question, since no length information can
be reconstructed using merely a pointer, we can not get
the length of str1 at compile-time. Use array based
string definition if compile time length is required.

If the problem can be reduced to find the dimension of an
char array, both your method and sizeof base methods
can do the same job in my mind.

BTW, compared with old sizeof(A)/sizeof(A[0]) macro,
GetStringLength can distinguish printer from array, which is
safer, but it is a function and has possible runtime cost.
Another way to do array dimension evaluation is

template<typename T, int N>
unsigned char (&DIM(T (&)[N]))[N];

#define SAFE_DIM(x) sizeof(DIM((x)))

Since operand to the above sizeof() operator is not evaluated,
no code will be generated.

Regards,

Jiang


--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Alberto Ganesh Barbati
PostPosted: Sat Sep 06, 2008 12:22 pm    Post subject: Re: How to get string length in compile-time?
       
Jiang ha scritto:
Quote:
On Sep 5, 6:38 pm, Alberto Ganesh Barbati <AlbertoBarb...@libero.it
wrote:
zade ha scritto:

[...]

template <int N
int GetStringLength(char const (&a)[N])
{
return N;
}

Be aware that this function counts the terminating null. If you don't
want to count the null, make it return N-1.


This function does not work for pointer-to-char-array and
fix size char array.

For pointer-to-char-array, as you said in your post, the answer cannot
be provided at compile time. For fixed-size char array, just provide an
overload without the "const" in it.

Quote:

So GetStringLength can be used to get the dimension of an array,
but this is not what zade really want in my mind.

Zade talked about string literals and my approach works for these:

int s = GetStringLength("hello, world");
assert(s == 13);

If zade had a different thing in mind, it wasn't clear enough.

Quote:
For the original question, since no length information can
be reconstructed using merely a pointer, we can not get
the length of str1 at compile-time. Use array based
string definition if compile time length is required.

If the problem can be reduced to find the dimension of an
char array, both your method and sizeof base methods
can do the same job in my mind.

BTW, compared with old sizeof(A)/sizeof(A[0]) macro,
GetStringLength can distinguish printer from array, which is
safer, but it is a function and has possible runtime cost.

What's the meaning of the word "printer" in the sentence above? I really
don't understand. Anyway, I don't see how my function could have a
runtime cost, given that it returns a constant expression. Any
reasonable compiler should be able to expand it inline.

Ganesh

--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Jiang
PostPosted: Sat Sep 06, 2008 8:23 pm    Post subject: Re: How to get string length in compile-time?
       
On Sep 6, 9:22 pm, Alberto Ganesh Barbati <AlbertoBarb...@libero.it>
wrote:
Quote:
Jiang ha scritto:


[...]

Quote:

So GetStringLength can be used to get the dimension of an array,
but this is not what zade really want in my mind.

Zade talked about string literals and my approach works for these:

int s = GetStringLength("hello, world");
assert(s == 13);

If zade had a different thing in mind, it wasn't clear enough.


Although I never see such kind of usage for string literal,
but maybe this is useful for zade.

Quote:
BTW, compared with old sizeof(A)/sizeof(A[0]) macro,
GetStringLength can distinguish printer from array, which is
safer, but it is a function and has possible runtime cost.

What's the meaning of the word "printer" in the sentence above? I really
don't understand.

It should read pointer, I apologize for the possible confusion.
If you really still can not understand the point here, ask here.

Quote:
Anyway, I don't see how my function could have a
runtime cost, given that it returns a constant expression. Any
reasonable compiler should be able to expand it inline.

Ok, for the original problem, what is the benefit?

int s = sizeof("hello, world");
assert(s == 13);

works just fine.

Regards,

Jiang



--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
zade
PostPosted: Sun Sep 07, 2008 4:28 pm    Post subject: Re: How to get string length in compile-time?
       
Quote:
Ok, for the original problem, what is the benefit?

int s = sizeof("hello, world");
assert(s == 13);
I don't know that the expresion "int s = sizeof("hello, world");" will

consume some time or not.
Let me give the actal problem in my code. I want to advance the
pointer to some certain size of the given string, just like below:
void some_func(const char* pos)
{
pos += strlen("geogcs");
}
I can write "pos += 6;" instead because I really know the length of
"geogcs" in compile time, but I think the better is "pos +=
GetStringLength<"geogcs">::value;". So I post my question.


--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Alberto Ganesh Barbati
PostPosted: Mon Sep 08, 2008 1:39 pm    Post subject: Re: How to get string length in compile-time?
       
zade ha scritto:
Quote:
Ok, for the original problem, what is the benefit?

int s = sizeof("hello, world");
assert(s == 13);
I don't know that the expresion "int s = sizeof("hello, world");" will
consume some time or not.

sizeof() is guaranteed to be evaluated at compile time.

Quote:
Let me give the actal problem in my code. I want to advance the
pointer to some certain size of the given string, just like below:
void some_func(const char* pos)
{
pos += strlen("geogcs");
}
I can write "pos += 6;" instead because I really know the length of
"geogcs" in compile time, but I think the better is "pos +=
GetStringLength<"geogcs">::value;". So I post my question.


The fact is that string literals are not allowed as template arguments.
See 14.3.2/2.

HTH,

Ganesh

--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

wczasy sylwestrowe Szklarska Poręba Konferencje torby hotele rzym strony www Kraków