gpt4 book ai didi

c++ - 如何以不同方式专门化 typedef 及其隐式类型?

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

我有这样的东西:

typedef int AnotherType;
template <typename T> Func( T Value );

// And I want to specialize these two cases separately:

template <> bool Func<int>( int Value ) {...}
template <> bool Func<AnotherType>( AnotherType Value ) {...}

我真的不需要专攻 int,我真正需要的是为 AnotherType 执行不同的函数。而且我无法更改 AnotherType 或基本函数的定义。

由于 SFINAE,重载也无济于事。

最佳答案

答案是否定的。当您使用 typedef 时,您为一个类型创建了一个别名,而不是一个实际类型本身。编译器将对两者一视同仁。这就是为什么:

typedef int Foo;
typedef int Bar;

Bar bar = 1;
Foo foo = bar;

将编译。它们都是整数。

关于c++ - 如何以不同方式专门化 typedef 及其隐式类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4832900/

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