gpt4 book ai didi

c++ - 从类型包中解压模板模板参数

转载 作者:搜寻专家 更新时间:2023-10-31 02:12:47 24 4
gpt4 key购买 nike

如何修复 using 行?

test_temp.cpp:

#include <memory>
template <typename A, typename B, template<typename> class C>
class X {};

template <typename A1, typename B1, template<typename> class C1>
class Type_bundle {
public:
typedef A1 A;
typedef B1 B;
template<typename T> using C = C1<T>;
};

template <typename T_b>
using X_b = X<typename T_b::A, typename T_b::B, typename T_b::template C>;


int main() {
typedef Type_bundle<int,float,std::allocator> Ttb;
X_b<Ttb> x;
}

来自 clang++ 的错误

test_temp.cpp:14:63: error: expected an identifier or template-id after '::'
using X_b = X<typename T_b::A, typename T_b::B, typename T_b::template C>;
~~~~~^

为什么这行不通?

最佳答案

当你写的时候:

template <typename T_b>
using X_b = X<typename T_b::A, typename T_b::B, typename T_b::template C>;
// ~~~~~~~~~

这表明后面将是一个类型的名称。但是T_b::template C不是一个类型,它是一个模板,所以这个结构是无效的。如果您继续将参数传递给 C,您将使用该构造, 比如 typename T_b::template C<D> .那里,两个typenametemplate是必要的。

你想要的只是:

template <typename T_b>
using X_b = X<typename T_b::A, typename T_b::B, T_b::template C>;

没有typename .

关于c++ - 从类型包中解压模板模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42303674/

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