c++ - 模板模板参数中的模板参数过多

标签 c++ templates

我正在尝试实现一些模板模板代码,如下所示,它在 GCC 7 上运行良好,但在 Clang 6 上编译失败。有什么想法吗?

#include <iostream>

template <template <typename, typename> class Op>
class Function
{
};

template <typename A, typename B, bool is_f = std::is_floating_point<A>::value || std::is_floating_point<B>::value > struct Operator;

template <typename A, typename B>
struct Operator<A, B, false>
{};


template <typename A, typename B>
struct Operator<A, B, true>
{};

using FunctionOperator = Function<Operator>;


int main(int argc, char * argv[]){
    std::cout << "hi!\n";
    return 0;
}

编译错误:

tmp.cpp:19:35: error: template template argument has different template parameters than its corresponding template template parameter
using FunctionOperator = Function<Operator>;
                                  ^
tmp.cpp:8:1: note: too many template parameters in template template argument
template <typename A, typename B, bool is_f = std::is_floating_point<A>::value || std::is_floating_point<B>::value > struct Operator;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tmp.cpp:3:11: note: previous template template parameter is here
template <template <typename, typename> class Op>
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

最佳答案

Clang 显然有问题,因为基本的 Operator 结构模板有 3 个而不是 2 个参数,因此拒绝在 Function 模板中接受它。 根据这个问题的回答Template template argument causes compiler error under Clang but not GCC Clang 是错误的,而 GCC 在这个问题上是符合标准的。无论如何,这是针对此问题的快速解决方法:

template <template <typename, typename> class Op>
class Function
{
};

template <typename A, typename B, bool is_f = std::is_floating_point<A>::value || std::is_floating_point<B>::value > struct Operator;

template <typename A, typename B>
struct Operator<A, B, false>
{};


template <typename A, typename B>
struct Operator<A, B, true>
{};

template<class A, class B>
using Op = Operator<A, B>;

using FunctionOperator = Function<Op>;


int main(int argc, char * argv[]){
    FunctionOperator o;
    std::cout << "hi!\n";
    return 0;
}

它适用于 gcc 和 clang。

关于c++ - 模板模板参数中的模板参数过多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51282912/

相关文章:

c++ - 在 Windows 8.1 中运行 Visual Studio 6 C++

c++ - 自由函数和成员函数的自定义 std::function-like 实现

javascript - meteor :如何使用模板事件检索 "{{this}}-value"

c++ - 使用 Boost.Locale 库检索代码点

c++ - 可以将 MFC 对话框资源附加到 CChildView 吗?

c++ - 如何在命令行设置 gcc 链接器输出节大小?

c++ - 如何控制使用 MPC 时清理的输出目录和文件?

javascript - 仅动态更改容器的选择性部分

c++ - 如何声明内部模板类?

java - 如何使用 Wordpress 运行 Java 页面?