gpt4 book ai didi

c++ - 类型定义非类型模板参数

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

我想在类之外访问模板参数。我通常这样做:

template <class T>
class A
{
typedef typename T T;
}

A<int>::T;

我希望能够对非类型模板参数执行相同的操作。这不起作用:

template <int T>
class A
{
typedef typename T T;
}

A<3>::T;

我会解释为什么我需要这个。我想定义第二个类如下:

template <class C>
class B
{
static int func() {return C::T;}
}

B<A<3> >::func();

正确的做法是什么?非常感谢。

最佳答案

那是因为 T 不是类型名称,您不能 typedef 它。它是一个 int 值,如果您想将它作为类的静态成员访问,则需要一个静态成员 int。看起来你真正想要的是这样的:

template <int T>
class A
{
public:
static const int x = T;
};

doSomething(A<5>::x);

关于c++ - 类型定义非类型模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14799312/

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