【发布时间】:2019-02-10 03:33:33
【问题描述】:
#define T int
int main ()
{
const T x = 2;
// (1) -- type of `x` is compared with `T`
static_assert(std::is_same<std::remove_const<decltype(x)>::type, T>::value, "Not same");
// (2) -- type of `x` is compared with `const T`
static_assert(std::is_same<std::remove_const<decltype(x)>::type, const T>::value, "Not same");
}
以上代码按预期工作。其中(1)通过,(2)失败。
但是,它会以其他方式发生,即。 (1) 失败和 (2) 通过,如果我进行以下更改:
#define T int& // <--- added reference
为什么会这样?
使用decltype的类似代码,我们可以在代码中添加什么,以便(1)通过引用和非引用类型,即int&和int?
也欢迎使用const_cast 的可能解决方案。
注意:由于我想对从对象中删除const 进行宏化处理;我用过decltype。
【问题讨论】:
标签: c++ c++11 constants decltype reference-type