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

R

Ramon F Herrera

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
 
R

Ron

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.
 
R

Ramon F Herrera

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
 
J

James Kanze

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.
 
S

Scooser

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);
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top