473,803 Members | 3,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(ch ar * source)
{
printf("Adddyna mic: %d\n",strlen(so urce));
}
void AppendString(co nst char * source,unsigned len)
{
printf("AddStat ic: %d\n",len);
}
myStr operator+=(char * source)
{
printf("Add Dynamic Length\n");
AppendString(so urce);
return *this;
}

__forceinline myStr operator+=(cons t 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(so urce,strlen(sou rce));
return *this;
}

};

int _tmain(int argc, _TCHAR* argv[])
{
myStr test;
test += "HHHHHHHHHHHHH" ; //= 13 bytes = 0xd
test.AppendStri ng("HHHHHHHHHHH HH",strlen("HHH HHHHHHHHHH"));
return 0;
}

thx

franz
Apr 11 '08 #1
3 2473
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(ch ar_arr) (sizeof(char_ar r)/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+=(cons t 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+=(cons t char * source,strlen(s ource)) <- where preprocessor
automatically does an inline expand ??

and why
3'rd chanche
as:

test.AppendStri ng("HHHHHHHHHHH HH",strlen("HHH HHHHHHHHHH"));

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

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

myStr operator+=(cons t char * source)
{
printf("Add Static Length\n");
AppendString(so urce,sizeof(sou rce)); <- 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.inva lidschrieb 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(ch ar_arr) (sizeof(char_ar r)/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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
2809
by: Adam Siler | last post by:
i want to display a series of inline, fixed-width DIVs that will wrap into columns - a table, basically. i can do it under Internet Explorer, but the same code under Netscape or Opera does not behave as expected. the DIVs "collapse" into small rectangles (i put a border around them so i could see), but their contents (some middle-aligned text) span the width of the page. if not for the borders that i added, it would appear that each DIV was...
20
3158
by: Grumble | last post by:
Hello everyone, As far as I understand, the 'inline' keyword is a hint for the compiler to consider the function in question as a candidate for inlining, yes? What happens when a function with extern linkage is inlined? Should the compiler still export the function? Or is an inlined function implicitly static?
17
2513
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: public static bool operator !=(MyType x, MyType y) { return !(x == y); } That way the == operator handles everything, and extra comparing logic isn't
4
4779
by: bor_kev | last post by:
Hi! What's the syntax to overload the operator= under Microsoft Visual C++ .NET 2005 in a managed class. I tried : static Myclass^ op_Assign (Myclass^, Myclass^){} but it doesn't work. don't know why....
5
4643
by: DaVinci | last post by:
/* how to overload the operation ,subscipt of 2D-array? * how can I get element through piece,rather than piece(i)? * */ #include"global.h" using namespace std; class Piece { public: Piece() {
6
9873
by: aj | last post by:
DB2 LUW 8.1 FP11 Can't I overload SPs based on param types rather than just number of params? If I do: CREATE PROCEDURE FOO( P_DATE_OLD DATE,P_DATE_NEW DATE) then CREATE PROCEDURE FOO(
32
3350
by: chris.fairles | last post by:
Just want an opinion. I have an algorithm that needs to run as fast as possible, in fact. Its already too slow. I've done as much algorithmic changes as I can to reduce the amount of code, so now I'm turning to micro-optimizations. One function that gets run a bazillion times is the pow() function from math.h. However I've realized probably 90% of the time, the exponent will be 0. 99.9% of the time, the exponent will lie between -3 and...
6
185
by: news.inode.at | last post by:
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;
9
2318
by: Anthony Williams | last post by:
Hi, Should the following compile, and what should it print? #include <memory> #include <iostream> void foo(std::auto_ptr<intx) { std::cout<<"copy"<<std::endl;
0
9703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9566
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10317
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2974
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.