gpt4 book ai didi

c++ - 模板模板代码不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:21:58 26 4
gpt4 key购买 nike

我正在使用 gcc/4.7,我需要在模板函数(或成员函数)中用模板模板参数实例化一个类。我收到以下错误

test.cpp: In function 'void setup(Pattern_Type&)':
test.cpp:17:34: error: type/value mismatch at argument 1 in template parameter list for 'template<template<class> class C> struct A'
test.cpp:17:34: error: expected a class template, got 'typename Pattern_Type::traits'
test.cpp:17:37: error: invalid type in declaration before ';' token
test.cpp:18:5: error: request for member 'b' in 'a', which is of non-class type 'int'

通过注释代码运行的片段中标记的两行,因此 A a 可以在“main”中实例化,但不能在“setup”中实例化。我认为这也会引起其他人的兴趣,我很乐意了解代码不起作用的原因。这是代码

struct PT {
template <typename T>
struct traits {
int c;
};
};

template <template <typename> class C>
struct A {
typedef C<int> type;
type b;
};

template <typename Pattern_Type>
void setup(Pattern_Type &halo_exchange) {
A<typename Pattern_Type::traits> a; // Line 17: Comment this
a.b.c=10; // Comment this
}

int main() {
A<PT::traits> a;

a.b.c=10;

return 0;
}

感谢任何建议和修复!毛罗

最佳答案

您需要将 Pattern_Type::traits 标记为模板:

A<Pattern_Type::template traits> a;

这是必需的,因为它依赖于模板参数 Pattern_Type

你也不应该在那里使用 typename 因为 traits 是一个模板,而不是一个类型。

关于c++ - 模板模板代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13974555/

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