测试std :: common_type是否存在 [英] Test if std::common_type exists
本文介绍了测试std :: common_type是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想编写一个帮助程序模板,以检查模板参数包是否具有通用类型,即,是否将 std :: common_type
应用于该包定义了一种类型。
I want to write a helper template that checks if a template parameter pack has a common type, i.e., if applying std::common_type
to the pack defines a type.
使用 std :: void_t 使用SFINAE,我得出以下定义:
Using std::void_t with SFINAE I came up with the following definition:
template<typename... Types, typename Enable = void>
struct has_common_type : std::false_type
{ };
template<typename... Types>
struct has_common_type<Types..., std::void_t<std::common_type<Types...>::type>> : std::true_type
{ };
但这不起作用,因为模板参数包必须是最后一个参数。编译器会引发以下错误:
This however does not work, because the template parameter pack must be the last parameter. The compiler raises the following error:
error: template parameter pack must be the last template parameter
template<typename... Types, typename Enable = void>
如何定义这样的模板?
推荐答案
选项#1
Option #1
template <typename... Ts>
using has_common_type = std::experimental::is_detected<std::common_type_t, Ts...>;
template <typename AlwaysVoid, typename... Ts>
struct has_common_type_impl : std::false_type {};
template <typename... Ts>
struct has_common_type_impl<std::void_t<std::common_type_t<Ts...>>, Ts...> : std::true_type {};
template <typename... Ts>
using has_common_type = typename has_common_type_impl<void, Ts...>::type;
这篇关于测试std :: common_type是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文