我试图将其归结为基本要素。
我有一个可变参数模板类 Foo,其中包含一个用其类型索引的对象“列表”。我使用函数 bar<U>()
提取该类型的元素。我使用可变模板和 std::enable_if 来解决这个问题,只定义 bar<U>()
其中 T == U。
然后我使用“using”从基类中公开所有“bar”函数。
#include <type_traits>
template<typename... Ts>
class Foo
{
public:
void bar() {}
};
template<typename T, typename... Ts>
class Foo<T, Ts...> : public Foo<Ts...>
{
public:
using Foo<Ts...>::bar;
template<typename U>
typename std::enable_if<std::is_same<U, T>::value, U >::type
bar()
{
return mObj;
}
private:
T mObj;
};
template<typename T>
void bar2()
{
Foo<int, float, double> list;
list.bar<T>();
}
int main()
{
bar2<float>();
return 0;
}
除 Clang 和 Visual Studio 2015 外,这非常有效。尝试了 MSVC 19.0 和 19.10,并给出了错误:
Compiled with /EHsc /nologo /W4 /c
main.cpp
main.cpp(30): error C2672: 'Foo<int,float,double>::bar': no matching overloaded function found
main.cpp(35): note: see reference to function template instantiation 'void bar2<float>(void)' being compiled
main.cpp(30): error C2770: invalid explicit template argument(s) for 'std::enable_if<std::is_same<U,T>::value,U>::type Foo<int,float,double>::bar(void)'
with
[
T=int
]
main.cpp(18): note: see declaration of 'Foo<int,float,double>::bar'
GCC 至少在 4.7-6.3 之间编译这个没问题。我首先认为这可能是因为 Visual Studio 2015 中缺少 c++11 的某些功能,但令人惊讶的是,这在较旧的 Visual Studio 2013 (MSVC 18.0) 中编译得很好。 Clang 也失败了。
所以我的问题是,这是这些编译器的缺点还是我在这里做的事情是不允许的?
如果我用像 list.bar<int>()
这样的硬编码类型调用“bar”它在所有经过测试的编译器上编译。
最佳答案
使用enable_if
在这里,你需要提供一个替代选项,当is_same<U, T>::value == false
.理想情况下,这可以做到 by exposing all base class members bar
with a using-declaration ...
using Foo<Ts...>::template bar;
不幸的是,标准禁止这样做,并且it was decided not to rectify this .因此,我们必须以另一种方式暴露它们。因此,最简单的解决方案是为 Foo<Ts...>::template bar()
制作一个包装器,如下:
template<typename T, typename... Ts>
class Foo<T, Ts...> : public Foo<Ts...>
{
public:
// using Foo<Ts...>::template bar; // Sadly, this is forbidden.
template<typename U>
typename std::enable_if<std::is_same<U, T>::value, U >::type
bar()
{
return mObj;
}
// Additional wrapper, to compensate for lack of valid syntax.
template<typename U>
typename std::enable_if<!std::is_same<U, T>::value, U >::type
bar()
{
return Foo<Ts...>::template bar<U>();
}
private:
T mObj;
};
但是请注意,包装器不能调用 Foo<Ts...>::bar()
, 由于它返回 void
.假设这是一般情况,打算在 U
时使用不是该包的成员,有两种方法可以纠正此问题:
修改
Foo<Ts...>::bar()
.template<typename... Ts> class Foo { public: template<typename T> T bar() { // Return an invalid value. return T{-1}; } };
提供第三个版本
Foo<T, Ts...>::bar()
, 在U
时使用不是T, Ts...
的成员;这个返回Foo<Ts...>::bar()
.为此,定义一个特征来检测它是否在包中会很有用。template<typename...> struct is_in_pack : std::false_type {}; template<typename U, typename T1, typename... Ts> struct is_in_pack<U, T1, Ts...> : std::integral_constant<bool, std::is_same<U, T1>::value || is_in_pack<U, Ts...>::value> {};
然后,我们只需要使用特征。
template<typename T, typename... Ts> class Foo<T, Ts...> : public Foo<Ts...> { public: // using Foo<Ts...>::template bar; // Sadly, this is forbidden. template<typename U> typename std::enable_if<std::is_same<U, T>::value, U >::type bar() { return mObj; } // Additional wrapper, to compensate for lack of valid syntax. // U is a member of <T, Ts...>. template<typename U> typename std::enable_if<!std::is_same<U, T>::value && is_in_pack<U, T, Ts...>::value, U >::type bar() { return Foo<Ts...>::template bar<U>(); } // Additional wrapper, to compensate for lack of valid syntax. // U isn't a member of <T, Ts...>. template<typename U> typename std::enable_if<!is_in_pack<U, T, Ts...>::value>::type bar() { return Foo<>::bar(); } private: T mObj; };
在这些选项中,我建议使用后者,因为它更符合您当前的代码。
关于c++ - 在 vs2015 的可变参数模板中找不到重载的成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42684405/