473,834 Members | 1,820 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 2475
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
2811
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
3161
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
2516
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
4780
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
9875
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
3352
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
9799
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...
1
10554
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10224
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
9338
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7761
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6960
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
5629
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3985
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3084
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.