variadic templates jeden typ

0

Nie mogę nigdzie znaleŹć w internecie, czy jest możliwe stworzenie klasy z variadic template a następnie utworzenie metody równiez z własnym szablonem przyjmującym tylko jeden dowolny typów z types...? coś w stylu

template<Types...>
class a 
{
template<oneof Types> 
int metoda();

};
3

Tak, jest możliwe.

	Foo<string,int,thread> f;
	f.foo<thread>();
	f.foo<int>();
	f.foo<string>();
//	f.foo<vector<char>>(); // error

http://melpon.org/wandbox/permlink/3YOIWw0bEOYDXFq8

możesz osiągnąć np. dzięki:

template<typename T, typename First, typename... Ts>
struct is_any_of
{
	static constexpr bool value = std::is_same<T, First>::value || is_any_of<T, Ts...>::value;
};

template<typename T, typename Last>
struct is_any_of<T, Last>
{
	static constexpr bool value = std::is_same<T, Last>::value;
};

template<typename... Ts>
struct Foo
{
	template<typename T, typename = typename std::enable_if<is_any_of<T, Ts...>::value, void>::type>
	void foo(){
		BARK;
	}
};

edit: ewentualnie możesz użyć Boost.MPL:
http://melpon.org/wandbox/permlink/pOLhJHcRNgDy4ZGM

template<typename... Ts>
struct Foo
{
	template<typename T,
			 typename = typename std::enable_if<
				 !std::is_same<
					 typename boost::mpl::find<boost::mpl::vector<Ts...>, T>::type,
					 typename boost::mpl::end<boost::mpl::vector<Ts...>>::type
				 >::value,
				 void
			 >::type>
	void foo(){
		BARK;
	}
};

1 użytkowników online, w tym zalogowanych: 0, gości: 1