我试图使用新的decltype关键字将一些代码移到模板中,但是当与取消引用的指针一起使用时,它会生成引用类型。SSCCE:
#include <iostream>
int main() {
int a = 42;
int *p = &a;
std::cout << std::numeric_limits<decltype(a)>::max() << '\n';
std::cout << std::numeric_limits<decltype(*p)>::max() << '\n';
}第一个numeric_limits工作,但第二个抛出一个value-initialization of reference type 'int&'编译错误。如何从指向该类型的指针中获取值类型?
发布于 2015-02-25 13:58:27
如果您要从指针转到指向类型,那么为什么要取消引用呢?只是,嗯,移除指针:
std::cout << std::numeric_limits<std::remove_pointer_t<decltype(p)>>::max() << '\n';
// or std::remove_pointer<decltype(p)>::type pre-C++14发布于 2015-02-25 13:49:12
您希望删除引用以及潜在的constness,所以您可以使用
std::numeric_limits<std::decay_t<decltype(*p)>>::max()https://stackoverflow.com/questions/28720632
复制相似问题