469,621 Members | 1,802 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,621 developers. It's quick & easy.

inline overload operation question

Sorry for this stupid question, but i am lost.

If i write an stringlib with += overload operators (no i do not, but my
thing is much more complicated) , and i have to precalculate the strlen() --
as seen in the example here
How do i solve this ?
struct myStr
{
private:
unsigned len;
unsigned size;
char string[100];
public:
myStr():
len(0),
size(0)
{
string[0] = 0;
}
~myStr() {}
void AppendString(char * source)
{
printf("Adddynamic: %d\n",strlen(source));
}
void AppendString(const char * source,unsigned len)
{
printf("AddStatic: %d\n",len);
}
myStr operator+=(char * source)
{
printf("Add Dynamic Length\n");
AppendString(source);
return *this;
}

__forceinline myStr operator+=(const char * source)
{
printf("Add Static Length\n");
// HELP !!!! --why does he not inline the function and precalculate
strlen() at compiletime ? is there a method ?
like: templates or so ?

AppendString(source,strlen(source));
return *this;
}

};

int _tmain(int argc, _TCHAR* argv[])
{
myStr test;
test += "HHHHHHHHHHHHH"; //= 13 bytes = 0xd
test.AppendString("HHHHHHHHHHHHH",strlen("HHHHHHHH HHHHH"));
return 0;
}

thx

franz
Apr 11 '08 #1
3 2168
Anyways, while that may be so, it would also be useful to have a strlen
macro. The compiler already knows the layout of the data, it's a
compile time invariant, and would allow things like conventions about
storing word dictionaries as offset/length pairs in large strings, with
no null-terminated storage, besides just enabling simple writes of those
things.

Where are the interminable discussions about strlen macro and why/why
not it is already standard?
I guess there is sizeof for array types that returns the size of the
array, but literals are generally const char* types.

sizeof("xyz") == sizeof(char*); // <- not strlen

char xyz[] = "xyz"

sizeof(xyz) == 4 ; // <- not strlen

So, maybe it is as simple as to declare the C strings as character
arrays, and subtract 1, where sizeof(char)==1.

#define strlen_macro(char_arr) (sizeof(char_arr)/sizeof(char)-1)

Yet, still I wonder about the correct const char* <-char[]
declarations, in terms of whether when the compiler sees a character
string literal, whether it is treated as a char* or char[] in terms of
sizeof (the operator/compiler facility). Does sizeof("xyz") always
return sizeof(char*), or sizeof(char)*4, as defined behavior?

Ross F.
Apr 11 '08 #2
Yet, still I wonder about the correct const char* <-char[]
declarations, in terms of whether when the compiler sees a character
string literal, whether it is treated as a char* or char[] in terms of
sizeof (the operator/compiler facility). Does sizeof("xyz") always
return sizeof(char*), or sizeof(char)*4, as defined behavior?
OK, modern C++ string literals are const char[], before they were
char[], so a strlen_macro is easy to implement.

Ross F.
Apr 11 '08 #3
i fully agree with your position of "why is there no macro for strlen() like
it is for sizeof() ...)

even if not, the 2'nd chance would be an abstraction to the operator+= like

myStr operator+=(const char * source)
myStr operator+=(char * source)
as compiler already sees operation like Str test += "hello"; as const char
operation (ok, this is cool)
but how to use this information then ?
why is there no thing like
myStr operator+=(const char * source,strlen(source)) <- where preprocessor
automatically does an inline expand ??

and why
3'rd chanche
as:

test.AppendString("HHHHHHHHHHHHH",strlen("HHHHHHHH HHHHH"));

really does a inline "pre-length" calculation (look to /o2)
and it is in ASM
test.AppendString("HHHHHHHHHHHHH",13)); <- cool

but why the hell can i not inline the += operator into it ????
and yes, you are right, doing this

myStr operator+=(const char * source)
{
printf("Add Static Length\n");
AppendString(source,sizeof(source)); <- sizeof == 4 now , because function
gets not inlined !!!
return *this;
}

this is all "sh.t"

you think there is no chance with a function template ? (i am not very good
in them ..) ?

franz
"Ross A. Finlayson" <ra*@tiki-lounge.com.invalidschrieb im Newsbeitrag
news:ft**********@aioe.org...
>
>Anyways, while that may be so, it would also be useful to have a strlen
macro. The compiler already knows the layout of the data, it's a compile
time invariant, and would allow things like conventions about storing
word dictionaries as offset/length pairs in large strings, with no
null-terminated storage, besides just enabling simple writes of those
things.

Where are the interminable discussions about strlen macro and why/why not
it is already standard?

I guess there is sizeof for array types that returns the size of the
array, but literals are generally const char* types.

sizeof("xyz") == sizeof(char*); // <- not strlen

char xyz[] = "xyz"

sizeof(xyz) == 4 ; // <- not strlen

So, maybe it is as simple as to declare the C strings as character arrays,
and subtract 1, where sizeof(char)==1.

#define strlen_macro(char_arr) (sizeof(char_arr)/sizeof(char)-1)

Yet, still I wonder about the correct const char* <-char[] declarations,
in terms of whether when the compiler sees a character string literal,
whether it is treated as a char* or char[] in terms of sizeof (the
operator/compiler facility). Does sizeof("xyz") always return
sizeof(char*), or sizeof(char)*4, as defined behavior?

Ross F.

Apr 11 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

30 posts views Thread by Adam Siler | last post: by
20 posts views Thread by Grumble | last post: by
17 posts views Thread by Chris | last post: by
4 posts views Thread by bor_kev | last post: by
5 posts views Thread by DaVinci | last post: by
32 posts views Thread by chris.fairles | last post: by
6 posts views Thread by news.inode.at | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.