【发布时间】:2019-03-23 21:35:24
【问题描述】:
在以下(最小化)代码中,我有一个公开的using 声明,它指的是decltype(something_private):using Foo = decltype(something_private<T>)。
在 Clang 而不是 GCC 上,由于它是私有的,因此无法编译。
问题:
- 如果我不想公开
func<T>(),有什么优雅的解决方案。 - 在 C++ 标准 (C++11) 中,备份 Clang 在哪里是正确的?
以下代码在 Clang (3.9 - 7.0) 上失败并出现以下错误代码,但在 GCC (4.8.4 - 8.2) 上构建:
class A {
private:
template <class T>
static auto func() -> T; // The actual return type is much
// more complicated, so `using Foo = T` would not work.
public:
template <class T>
using Foo = decltype(func<T>());
};
int main(int, char**) {
A::Foo<int> y;
return y;
}
Clang 7.0 输出:
<source>:10:24: error: 'func' is a private member of 'A'
using Foo = decltype(func<T>());
^~~~~~~
<source>:14:7: note: in instantiation of template type alias 'Foo' requested here
A::Foo<int> y;
^
<source>:6:15: note: declared private here
static auto func() -> T;
^
1 error generated.
Compiler returned: 1
【问题讨论】:
标签: c++ c++11 templates language-lawyer decltype