【问题标题】:clang error while compiling template [duplicate]编译模板时出现clang错误[重复]
【发布时间】:2017-11-28 21:03:02
【问题描述】:

我正在尝试接触 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 函数不是模板时,一切正常...

有什么建议吗?

【问题讨论】:

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


【解决方案1】:

应该是

return _this.template Bar<true>();

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

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

【讨论】:

  • 在这种情况下有效,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-04
  • 1970-01-01
  • 2021-04-18
  • 1970-01-01
相关资源
最近更新 更多