作为一般规则,decltype
保留 constness:
const int ci = 0;
decltype(ci) x; // x is const int
x = 5; // error--x is const
class Gadget{}:
const Gadget makeCG(); // factory
decltype(makeCG()) y1, y2; // y1 and y2 are const Gadgets
y1 = y2; // error--y1 is const
但是对于const
返回基本类型的返回类型,decltype
似乎扔掉const
了:
const int makeCI(); // factory
decltype(makeCI()) z; // z is NOT const
z = 5; // okay
在这种情况下为什么要decltype
丢弃 constness?我的意思是两个方面的问题:
- 标准的哪一部分规定了这种行为?
- 以这种方式指定行为的动机是什么?
谢谢。