c++ - 编译模板时出现 clang 错误

标签 c++ templates clang c++17 clang++

<分区>

我正在尝试接触 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;
}

这里失败了:

return _this.Bar<true>();

错误信息是:

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();
         ^    

这是我编译它的方式:

clang++-5.0 main.cpp -std=c++17

一些额外的信息。最新语言版本的Visual Studio 17可以吃这个。当 bar 函数不是模板时,一切正常...

有什么建议吗?

最佳答案

应该是

return _this.template Bar<true>();

在你的情况下 _this 有依赖类型。要引用其成员模板,您必须显式使用关键字 template

Where and why do I have to put the "template" and "typename" keywords?

关于c++ - 编译模板时出现 clang 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47540874/

相关文章:

c++ - 使用 CUDA 在主机设备中将 char 转换为 int

C++ static const struct 初始化

c++ - thrust::tuple in reduction 的自定义最小运算符

javascript - 如何处理 Mustache 模板中的 IF 语句?

c++ - 解决缺乏部分成员特化和特定性能设计约束的问题

c++ - 如何绕过 C++ 无法使用 lambda 匹配模板中的函数类型

c++ - 使用 lldb 调试时,较高的行号未解析为断点

c++ - GCC/Clang 未优化静态全局变量

constants - 如何在 LLVM IR 中初始化常量向量?

c++ - 为什么通过右值初始化非常量引用有效(在 C++11 中)?