我试图在一个可变参数派生类的所有基类上“迭代”,并调用一个名为“ stream”的方法(如果存在)。
为了检查一种方法是否存在,我使用了sfinae,它可以工作(请参见注释掉的代码)。但是,当我将其与可变参数模板“迭代”结合使用(如果不起作用)时,您的错误有点像是可变参数魔术中的sfinae部分突然无法正常工作。
帮助表示赞赏。我使用的是gcc 5.3.0。
#include <type_traits>
#include <iostream>
namespace detail{
template<class> struct sfinae_true : std::true_type{};
template<class T, class A0, class A1> static auto test_stream( int) -> sfinae_true<decltype(
std::declval<T>().stream(std::declval<A0>(), std::declval<A1>()))>;
template<class, class A0, class A1> static auto test_stream(long) -> std::false_type;
}
template<class T, class A0, class A1> struct has_stream : decltype(detail::test_stream<T, A0, A1>(0)){};
struct X{ void stream(int, bool){} };
struct A{ void stream(int, bool){} };
struct Y{};
template <typename ... T> class Z : public T ... {
public:
void ff() {
std::initializer_list<bool> {
( has_stream<T,int,bool>() ? (T::stream(0, 0) , true) : false) ...
};
}
};
int main(){
Z<X,A> a;
Z<X,A,Y> b;
/* this works as expected.
// this runs
if (has_stream<X, int, bool>()) {
std::cout << "has int, bool" << std::endl;
}
// and this doesn't
if (has_stream<Y, int, long>()) {
std::cout << "has int long" << std::endl;
}
*/
a.ff(); // no error
b.ff(); // error
}
$ g++ --std=c++14 -O0 2.cpp
2.cpp: In instantiation of ‘void Z<T>::ff() [with T = X, A, Y]’:
2.cpp:41:10: required from here
2.cpp:22:52: error: ‘stream’ is not a member of ‘Y’
( has_stream<T,int,bool>() ? (T::stream(0, 0) , true) : false) ...
^
2.cpp:21:9: error: no matching function for call to ‘std::initializer_list<bool>::initializer_list(<brace-enclosed initializer list>)’
std::initializer_list<bool> {
^
使用由您的类型特征组成的标记分派:
void ff()
{
std::initializer_list<int> {
(call<T>(has_stream<T,int,bool>{}), 0)...
};
}
template <typename U>
void call(std::true_type)
{
U::stream(0, 0);
}
template <typename U>
void call(std::false_type) {}
或使用表达式SFINAE:
void ff()
{
std::initializer_list<int> {
(call<T>(0), 0)...
};
}
template <typename U>
auto call(int) -> decltype(U::stream(0, 0), void())
{
U::stream(0, 0);
}
template <typename U>
void call(char) {}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句