一个关于标准转换的问题,supermegaboy等请进
原问题:http://tieba.baidu.com/p/1285092826
现在的问题是我不清楚我的解释是否完全正确。
我找过N3290全文的array-to-pointer conversion出现的地方;3.10;以及Clause 5的所有conversion和rvalue出现的地方,但没有发现ISO C++保证内建操作符在哪些确定情况下应用这些lvalue transformation。这样,按ISO C++(C++03也一样)就无法完全解决这里的问题——如何断定表达式"12"+12是合法的。
是ISO C++的defect,还是我看漏了?
[解决办法]
试试
#define TEMP(X) #X
#define COUT(X) \
cout <<"literal:\t" <<TEMP(X) <<endl; \
cout <<"type_id:\t" <<typeid(X).name() <<endl; \
cout <<"cout_val:\t" <<X <<endl; \
cout <<"size_of:\t" <<sizeof(X) <<endl; \
cout <<"- - - - -" <<endl <<endl
#define S1 "1234567890"
#define S2 "1234567890"+1
#define S3 "1234567890"+1+1
#define S4 "1234567890"+1+1+1
#define S5 "1234567890"+1+1+1+1
int main()
{
COUT(S1);
COUT(S2);
COUT(S3);
COUT(S4);
COUT(S5);
return 0;
}
literal: "1234567890"
type_id: char const [11]
cout_val: 1234567890
size_of: 11
- - - - -
literal: "1234567890"+1
type_id: char const [11]
cout_val: 234567890
size_of: 11
- - - - -
literal: "1234567890"+1+1
type_id: char const *
cout_val: 34567890
size_of: 4
- - - - -
literal: "1234567890"+1+1+1
type_id: char const *
cout_val: 4567890
size_of: 4
- - - - -
literal: "1234567890"+1+1+1+1
type_id: char const *
cout_val: 567890
size_of: 4
- - - - -
请按任意键继续. . .
literal: "1234567890"
type_id: A11_c
cout_val: 1234567890
size_of: 11
- - - - -
literal: "1234567890"+1
type_id: PKc
cout_val: 234567890
size_of: 4
- - - - -
literal: "1234567890"+1+1
type_id: PKc
cout_val: 34567890
size_of: 4
- - - - -
literal: "1234567890"+1+1+1
type_id: PKc
cout_val: 4567890
size_of: 4
- - - - -
literal: "1234567890"+1+1+1+1
type_id: PKc
cout_val: 567890
size_of: 4
- - - - -
Process returned 0 (0x0) execution time : 0.406 s
Press any key to continue.
(generally) the destination type. See 8.5, 8.5.3.
—end note]