简体   繁体   English

C ++ 0x decltype无法推断出成员变量constness

[英]C++0x decltype fails to deduce member variable constness

Consider the following code: 考虑以下代码:

template <typename T>
class B
{
};

template <typename T>
B<T> f(T& t)
{
    return B<T>();
}

class A
{
    class C {};
    C c;
public:
    A() {}

    decltype(f(c)) get_c() const { return f(c); }
};

int main()
{
    A a;
    a.get_c();
}

When I try to compile this, I get the error: 当我尝试对此进行编译时,出现错误:

test.cpp: In member function 'B<A::C> A::get_c() const':
test.cpp:31:46: error: conversion from 'B<const A::C>' to non-scalar type 'B<A::C>' requested

It seems that in the decltype, the compiler doesn't know that this is a const member function and therefore c is of type const C , and as a result incorrectly deduces the type of f(c) to be B<C> rather than B<const C> which is what it really is. 似乎在decltype中,编译器不知道这是const成员函数,因此cconst C类型,结果错误地将f(c)的类型推导为B<C>而不是B<const C>确实如此。

Am I doing something incorrectly, or is this a compiler bug? 我做错了什么,还是编译器错误? I use gcc 4.6, but 4.4 and 4.5 exhibit the same behaviour. 我使用gcc 4.6,但4.4和4.5表现出相同的行为。

The compiler operates correctly according to the current C++0x WP. 编译器根据当前的C ++ 0x WP正确运行。 See this issue report , which is currently being worked on. 请参阅此问题报告 ,该报告目前正在处理中。

Possibly the final C++0x Standard won't change the meaning of your decltype application in the return type before the function name. 最终的C ++ 0x Standard可能不会在函数名称之前的返回类型中更改decltype应用程序的含义。 You would need to move it to after the parameter list using -> decltype(f(c)) , which hopefully will do The Right thing in final C++0x. 您可能需要使用-> decltype(f(c))将其移动到参数列表之后,这有望在最终的C ++ 0x中做正确的事情。

No, decltype is not supposed to take into account whether the function is const or not, because it can't. 不, decltype不应考虑该函数是否为const,因为它不能。 The same could have been written differently: 相同的写法可能有所不同:

typedef decltype(f(c)) return_type;

return_type get_c() const { return f(c); }

Correction: decltype(f(c)) shouldn't even compile, because c is not static. 更正: decltype(f(c))甚至不应该编译,因为c不是静态的。

f需要采用右值引用,而不是左值引用。

I don't think you're allowed to use decltype on anything you wouldn't normally be able to call. 我认为您不可以在通常无法调用的任何内容上使用decltype。 I haven't been able to find anything in the standard that would allow you to access c, even within a decltype expression, outside of anywhere you could use c. 我无法在标准中找到任何东西,即使在decltype表达式内,也无法在可以使用c的任何地方访问c。 Since you don't have a this pointer at the point you're trying to do your thing, I don't think you can do what you're trying to do. 由于您在尝试做某事时没有this指针,因此我认为您无法做您想做的事情。 Doing so doesn't work in MSVC 2010 at least, and it has nothing to do with const. 这样做至少在MSVC 2010中不起作用,并且与const无关。

I considered using declval to get one but you can't access A&&.c because A is an incomplete type at that point. 我考虑过使用declval来获取一个,但是您无法访问A &&。c,因为此时A是不完整的类型。 I can't see anyway to do what you're trying to do other than something like so: 无论如何,我看不到要执行您正在尝试做的事情:

decltype(f(declval<C const>())) get_c() const { ... }

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

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