Kon*_*kin 1 c++ templates clang clang++ c++17
我试图触及C++ 17的功能,我选择了clang.这是我的代码的简化示例,无法通过clang编译:
#include <iostream>
#include <limits>
template<
typename T,
template<typename T_> typename Final>
class Base
{
public:
decltype(auto) Foo() const noexcept
{
using TFinal = Final<T>;
auto& _this = static_cast<const TFinal&>(*this);
return _this.Bar<true>();
}
};
template<typename T>
class Derived :
public Base<T, ::Derived>
{
public:
template<bool min>
T Bar() const noexcept
{
return min ?
std::numeric_limits<T>::lowest() :
std::numeric_limits<T>::max();
}
};
int main()
{
Derived<int> instance;
auto result = instance.Foo();
std::cout << result << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它失败了:
return _this.Bar<true>();
Run Code Online (Sandbox Code Playgroud)
和错误消息是:
main.cpp:14:32: error: expected expression
return _this.Bar<true>();
^
main.cpp:35:10: error: variable has incomplete type 'void'
auto result = instance.Foo();
^
Run Code Online (Sandbox Code Playgroud)
以下是我编译它的方法:
clang++-5.0 main.cpp -std=c++17
Run Code Online (Sandbox Code Playgroud)
一些额外的信息.具有最新语言版本的Visual Studio 17可以吃这个.当bar函数不是模板时,一切都很好......
有什么建议这里有什么问题?
应该
return _this.template Bar<true>();
Run Code Online (Sandbox Code Playgroud)
在你的情况下_this有依赖类型.要引用其成员模板,您必须template明确使用该关键字.
我必须在何处以及为何要使用"模板"和"typename"关键字?
| 归档时间: |
|
| 查看次数: |
160 次 |
| 最近记录: |