Bug or feature? Are 'char' and "string" fully compatible inconcatenation?

Discussion in 'C++' started by Ramon F Herrera, Sep 16, 2009.

  1. This is posted only out of curiosity, because as you will see, the
    problem (bug? feature?) can be easily worked around.

    I am coding some parsing regular expressions, and discovered that
    trying to concatenate two consecutive 'chars' is not liked by the
    compiler:

    const char left_paren = '(';
    const char right_paren = ')';
    const line = "[AbcDef]";

    This type of expression is not good:

    const string expression = left_parent + left_paren + line +
    right_paren + right_paren;

    but it can be fixed like this:

    const temp = left_paren + line + right_paren;
    const expression = left_paren + temp + right-paren;

    The expression may be also fixed by using strings instead of chars:

    const string left_paren = "(";
    const string right_paren = ")";

    TIA,

    -Ramon
    Ramon F Herrera, Sep 16, 2009
    #1
    1. Advertising

  2. Ramon F Herrera

    Ron Guest

    On Sep 16, 3:06 pm, Ramon F Herrera <> wrote:
    > This is posted only out of curiosity, because as you will see, the
    > problem (bug? feature?) can be easily worked around.
    >
    > I am coding some parsing regular expressions, and discovered that
    > trying to concatenate two consecutive 'chars' is not liked by the
    > compiler:


    You can not concatenate chars or arrays of them, only (std::) strings
    have concatenation (but will allow the other types to be concatenated
    to std::string).

    >
    > const char left_paren      = '(';
    > const char right_paren    = ')';
    > const line                          = "[AbcDef]";


    The last line isn't even legal, no type. Presumably you
    want either:
    const char* line = "[abcdef]";
    or
    const char[] line = "[abcdef]";

    >
    > This type of expression is not good:
    >
    > const string expression = left_parent + left_paren + line +
    > right_paren + right_paren;
    >

    None of the stuff in the expression on the right side of the
    = is a std::string.

    > but it can be fixed like this:
    >
    > const temp = left_paren + line + right_paren;
    > const expression = left_paren + temp + right-paren;


    This again is illegal. Use types.

    >
    > The expression may be also fixed by using strings instead of chars:
    >
    > const string left_paren   = "(";
    > const string right_paren = ")";
    >
    >


    Correct. chars can be concated to strings and that yields a string.
    char arrays can be concated to strings and that yields a string.

    For every + you have, one side or both must be a string.
    Ron, Sep 16, 2009
    #2
    1. Advertising

  3. On Sep 16, 3:36 pm, Ron <> wrote:
    > On Sep 16, 3:06 pm, Ramon F Herrera <> wrote:
    >
    > > This is posted only out of curiosity, because as you will see, the
    > > problem (bug? feature?) can be easily worked around.

    >
    > > I am coding some parsing regular expressions, and discovered that
    > > trying to concatenate two consecutive 'chars' is not liked by the
    > > compiler:

    >
    > You can not concatenate chars or arrays of them, only (std::) strings
    > have concatenation (but will allow the other types to be concatenated
    > to std::string).
    >
    >
    >
    > > const char left_paren      = '(';
    > > const char right_paren    = ')';
    > > const line                          = "[AbcDef]";

    >
    > The last line isn't even legal, no type.  Presumably you
    > want either:
    >   const char* line = "[abcdef]";
    > or
    >   const char[] line = "[abcdef]";
    >
    >
    >
    > > This type of expression is not good:

    >
    > > const string expression = left_parent + left_paren + line +
    > > right_paren + right_paren;

    >
    > None of the stuff in the expression on the right side of the
    > = is a std::string.
    >
    > > but it can be fixed like this:

    >
    > > const temp = left_paren + line + right_paren;
    > > const expression = left_paren + temp + right-paren;

    >
    > This again is illegal.  Use types.
    >
    >
    >
    > > The expression may be also fixed by using strings instead of chars:

    >
    > > const string left_paren   = "(";
    > > const string right_paren = ")";

    >
    > Correct.  chars can be concated to strings and that yields a string.
    > char arrays can be concated to strings and that yields a string.
    >
    > For every + you have, one side or both must be a string.



    My posting had a couple of typos, Ron.

    For instance:

    const string line = "[AbcDef]";

    Thanks for the explanation.

    -Ramon
    Ramon F Herrera, Sep 16, 2009
    #3
  4. On Sep 16, 3:36 pm, Ron <> wrote:

    > For every + you have, one side or both must be a string.


    Therefore: it is a feature. The world makes sense again...

    Thx,

    -RFH
    Ramon F Herrera, Sep 16, 2009
    #4
  5. Ramon F Herrera

    James Kanze Guest

    On Sep 17, 7:07 am, Paavo Helde <> wrote:
    > Ron <> kirjutas:
    > [...]
    > > For every + you have, one side or both must be a string.


    > Not exactly. It is enough if at least one side of the first + is a
    > string. E.g. this should work:


    > const string expression = string() + left_paren + left_paren + line +
    > right_paren + right_paren;


    That's what he said. For the first +, you have a string as the
    left operand. For the second, too, since the left operand is
    the string which results from the first plus. And so on. Other
    variants which will work are:

    string const expression
    = left_paren + string( left_parent ) + line + right_paren +
    right_paren ;
    string const expression
    = string( left_paren ) + left_parent + line + right_paren +
    right_paren ;

    and so on.

    In practice, the simplest and most logical solution, of course,
    is just to declare all of the intermediate objects with type
    string. About the only time I wouldn't do this is if they were
    static objects and I wanted static initialization, to avoid any
    order of creation issues.

    --
    James Kanze
    James Kanze, Sep 17, 2009
    #5
  6. Ramon F Herrera

    Scooser Guest

    Hi,
    make sure that always operator+(const std:string&, ??? ) or operator+
    ( ??? , const std::string&) is called. To do this simply write

    const std::string expression (left_paren + ( left_paren + line +
    right_paren ) + right_paren);
    Scooser, Sep 18, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Emmanuelle Girodet

    JCredo JDO becomes fully Oracle compatible

    Emmanuelle Girodet, Jul 29, 2004, in forum: Java
    Replies:
    0
    Views:
    321
    Emmanuelle Girodet
    Jul 29, 2004
  2. max
    Replies:
    5
    Views:
    599
    Malcolm
    Jan 6, 2006
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,015
    Ian Collins
    May 9, 2006
  4. pantagruel
    Replies:
    0
    Views:
    227
    pantagruel
    Feb 17, 2006
  5. revek
    Replies:
    15
    Views:
    154
    Thomas 'PointedEars' Lahn
    Feb 2, 2009
Loading...

Share This Page