inline overload operation question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • news.inode.at

    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


  • Ross A. Finlayson

    #2
    Re: inline overload operation question

    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.

    Comment

    • Ross A. Finlayson

      #3
      Re: inline overload operation question

      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.


      Comment

      • Lehner Franz

        #4
        Re: inline overload operation question

        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" <raf@tiki-lounge.com.inva lidschrieb im Newsbeitrag
        news:ftolj0$u5c $1@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.

        Comment

        Working...