简体   繁体   English

无法在TDM-GCC的typeid中使用declval

[英]Unable to use declval in typeid for TDM-GCC

Compiler: TDM-GCC-5.1.0 (SJLJ unwinding) 编译器:TDM-GCC-5.1.0(SJLJ展开)

I was playing around with declval and I noticed that I was unable to use it in a context where it should work: as an argument to typeid() . 我在玩declval发现我无法在应该使用的上下文中使用它:作为typeid()的参数。

In the following code, I use declval for one of it's primary use cases: to obtain the return type of a method without going through an instance. 在以下代码中,我将declval用于其主要用例之一:在不通过实例的情况下获取方法的返回类型。 The error I get is the static_assert message of declval , but that should be impossible because typeid() doesn't evaluate it's argument in this case: 我得到的错误是declval的static_assert消息,但这应该是不可能的,因为在这种情况下, typeid()不评估其参数:

#include <typeinfo>
#include <utility>

struct Foo
{
    int func();
};

int main()
{
    typeid(std::declval<Foo>().func());
}

This doesn't compile for me (when compiled with -std=c++14 ). 这对我来说不是编译的(使用-std=c++14编译时)。 My only guess is that either I've found a compiler bug, or I've done something obviously wrong and I can't see it. 我唯一的猜测是我发现了编译器错误,或者做错了明显的事而看不到它。 If it is the latter, my apologies. 如果是后者,我表示歉意。

EDIT: Thanks to ildjarn for helping me out, the solution is to use decltype , so the last line of code becomes: 编辑:感谢ildjarn的帮助,解决方案是使用decltype ,因此代码的最后一行变为:

typeid(decltype(std::declval<Foo>().func()));

and this works nicely. 这很好用。 However, now my question becomes: how come? 但是,现在我的问题变成:怎么来? Both typeid() and decltype() are unevaluated contexts, so I'm not sure what the difference is. typeid()decltype()都是未评估的上下文,因此我不确定有什么区别。

It's a compiler bug. 这是一个编译器错误。

The solution around it is to use decltype() around the expression. 解决方案是在表达式周围使用decltype() Both decltype() and typeid() (in this case of a non-polymorphic-glvalue expression) are unevaluated contexts, which shouldn't make a difference, which is what makes this a bug. decltype()typeid() (在这种情况下是非多态的glvalue表达式)都是未评估的上下文,不应有任何区别,这就是导致此错误的原因。 Using decltype() here acts as a sort of "unevaluated context buffer", and somehow typeid() likes this better. 在这里使用decltype()就像是一种“未评估的上下文缓冲区”,某种程度上, typeid()喜欢这种方式。

Well, time to contact TDM about this. 好了,有时间联系TDM了。 This bug isn't TDM's issue, it's a vanilla bug (thanks ildjarn). 这个错误不是TDM的问题,而是一个香草错误(感谢ildjarn)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM