gpt4 book ai didi

c++ - Visual Studio 2013 中的推导失败,第一个模板参数是特定类型(这又是任意模板化的)

转载 作者:行者123 更新时间:2023-11-28 00:20:44 25 4
gpt4 key购买 nike

如果 Argument 是具有特定第一个模板参数(任意模板化)的特定模板,我想要一个特定的函数来进行计算。

考虑这些类

template<class S> struct A { };
template<class S> struct B { };
template<class S> struct C { };
template<class S, class U = B<S>> struct D { };

我试图通过以下方式实现我的目标

template<template<class ... X> class Y, class Z>
inline void foo(Y<A<Z>> const &av) { std::cout << "2\n"; }

问题:MSVS 2013 无法推断 Y .

int main()
{
foo(C<A<int>>()); // prints 2 as intended
foo(D<A<int>>()); // does NOT compile in VS 2013
return 0;
}

错误的原因(根据MSVS)是:

template-Argument for const Y<A<Z>> & cannot be deduced from D<A<int>, B<S>> with S=A<int>.

我的目标是编写处理任何给定类型的重载/特化 Y其中 Y::value_type/Y 的第一个模板参数可以是任何 A<T>要保留 foo 签名的位置:void foo (Y const &);

这是 MSVS 中的 Bug(因为 foo(D<A<int>>()); 实际上使用 g++ 打印 2)还是我遗漏了什么?

PS:圣诞快乐,如果你关心...

最佳答案

D<A<int>, B<S>>不是 Y<A<Z>> ,你必须添加额外的参数:

template<template<class...> class Y, class Z, typename ... Ts>
inline void foo(Y<A<Z>, Ts...> const &av) { std::cout << "2\n"; }

在哪里Ts...可以为空。

另一种方法是创建一个别名:

template <typename T>
using E = D<A<T>>;

然后

foo(E<A<int>>()); // OK, but still can't use foo(D<A<int>>());

关于c++ - Visual Studio 2013 中的推导失败,第一个模板参数是特定类型(这又是任意模板化的),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27644210/

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