You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将结构体的成员模板类型别名作为模板模板参数使用

正确语法

B<A::template type> b;

错误原因说明

  • 所有错误写法中额外添加的typename是核心问题:typename关键字的作用是告知编译器依赖作用域下的名称是具体类型,而你需要传递的是模板本身,不是类型,不需要加typename修饰。
  • 不需要在template后额外补充<typename>这类模板参数声明,A::template type已经可以明确表示这是结构体A内的模板别名,完全匹配B的模板模板参数要求。

完整可编译验证示例

#include <type_traits>

struct A
{
   template <typename T>
   using type = int;
};

template <template <typename> class C>
struct B
{
   using test_type = C<double>;
   static_assert(std::is_same_v<test_type, int>);
};

B<A::template type> b; // 编译通过

内容的提问来源于stack exchange,提问作者Y.D.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 12:57:03