gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:22:16 33 4
gpt4 key购买 nike

我正在尝试实现一些模板模板代码,如下所示,它在 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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com