【发布时间】:2014-12-13 15:08:56
【问题描述】:
以下程序会产生带有clang 的编译错误,尽管它会传递给其他编译器:
#include <utility>
struct foo
{
auto bar() -> decltype(0)
{
return 0;
}
using bar_type = decltype(std::declval<foo>().bar());
};
int main()
{
return 0;
}
clang 产生:
$ clang -std=c++11 clang_repro.cpp
clang_repro.cpp:10:48: error: member access into incomplete type 'foo'
using bar_type = decltype(std::declval<foo>().bar());
^
clang_repro.cpp:3:8: note: definition of 'foo' is not complete until the closing '}'
struct foo
^
1 error generated.
这个程序是否非法,如果是,是否有正确的方法来定义foo::bar_type?
clang详情:
$ clang --version
Ubuntu clang version 3.5-1ubuntu1 (trunk) (based on LLVM 3.5)
Target: x86_64-pc-linux-gnu
Thread model: posix
【问题讨论】:
-
Visual Studio 提供了相同的错误(在语言上有一些微小的差异)。我认为您更好的选择是在函数声明之前为类型加上别名,然后将别名类型用于您的函数以及您想到的任何其他目的。
-
感谢您的观点。我想到的实际用例(
bar是带有参数的成员函数模板)可能会排除该策略。 -
嗯,如果
bar是模板,则指向成员的指针也不起作用。在静态成员中做实际工作并让非静态成员简单地成为一个完美的转发包装器怎么样?然后你可以在静态成员上使用你想要的所有decltype。 -
对,这种解决方法可能就可以了。但是,我希望有一个不涉及太多间接和代码重复的解决方案。最直接的尝试却不能开箱即用,这有点尴尬。
-
这件事对我来说没有意义。您想在类中为函数模板的返回类型生成类型别名,并且所述函数模板的返回类型由模板参数确定?这在逻辑上是行不通的。由于函数模板的多个实例化,bar_type 将同时表示多个类型,这是 c++ 所不允许的。
标签: c++ clang return-type-deduction