gpt4 book ai didi

c++ - 模板构建 : compose templates from templates

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:47:53 25 4
gpt4 key购买 nike

是一种组合 template<int> 的方法来自 template<int, int>

我已经尝试了下面的代码,但是它没有编译:

#include <iostream>
template<int N, int M>
struct A { enum E { n = N, m = M }; };

template<template<int> class C>
struct B : public C<8> { };

int main(int argc, const char *argv[])
{
typedef B< A<4> > T;
T b;

std::cout << T::n << std::endl;
std::cout << T::m << std::endl;
return 0;
}

错误:

test3.cxx: In function ‘int main(int, const char**)’:
test3.cxx:10:23: error: wrong number of template arguments (1, should be 2)
test3.cxx:3:12: error: provided for ‘template<int N, int M> struct A’
test3.cxx:10:25: error: template argument 1 is invalid
test3.cxx:10:28: error: invalid type in declaration before ‘;’ token
test3.cxx:13:22: error: ‘T’ is not a class or namespace
test3.cxx:14:22: error: ‘T’ is not a class or namespace

最佳答案

下面的代码打印出 4 和 8,我希望我正确地推断出你的意思。您的模板模板中的参数数量与您传入的模板数量不匹配。

#include <iostream>
template<int N, int M>
struct A { enum E { n = N, m = M }; };

template<template<int, int> class C, int Num>
struct B : public C<Num, 8> { };

int main(int argc, const char *argv[])
{
typedef B< A, 4 > T;
T b;

std::cout << T::n << std::endl;
std::cout << T::m << std::endl;
return 0;
}

关于c++ - 模板构建 : compose templates from templates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8764653/

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