0

如果 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)是:

模板参数const Y<A<Z>> &不能从D<A<int>, B<S>>with推导出来S=A<int>

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

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

PS:如果你在乎的话,圣诞快乐...

4

1 回答 1

3

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>>());
于 2014-12-25T05:13:11.133 回答