关于C++ string::size_type的问题

因为用来改优先级的括号只能括表达式。这是个声明:string::size_type pos = input.find(",")这是个表达式:pos = input.find(",")
■网友
跟size_type没关系,你写 while ( (int x = foo()) != -1) 一样编译不过。
■网友
一、Primary expressions 定义The operands of any operator may be other expressions or primary expressions (e.g. in 1+2*3, the operands of operator+ are the subexpression 2*3 and the primary expression 1).
Primary expressions are any of the following:
1) Literals (e.g. 2 or "Hello, world")2) Suitably declared unqualified identifiers (e.g. n or cout)3) Suitably declared qualified identifiers (e.g. std::string::npos)4) Lambda-expressionsAny expression in parentheses is also classified as a primary expression: this guarantees that the parentheses have higher precedence than any operator.
二、测试
1、if里有变量声明
if ( string::size_type pos = input.find ( "," ) ) {
//可行
}
2、多一对括号
if ( ( string::size_type pos = input.find(",") ) ) {
//error: expected primary-expression before \u0026#39;pos\u0026#39;
}
三、结论
Any expression in parentheses is also classified as a primary expression
【关于C++ string::size_type的问题】 括号表达式需要是基本表达式(primary expression),分类包含上面一中的1)—4),可看出不包括变量声明语句。

■网友
因为定义变量不是表达式,所以不能做子表达式


    推荐阅读