是否可以将'enable

编程入门 行业动态 更新时间:2024-10-26 19:32:34
是否可以将'enable_if'和'is_same'与可变参数函数模板一起使用?(Is it possible to use 'enable_if' and 'is_same' with variadic function templates?)

这两个非可变函数模板进行编译:

template <typename T, typename U> typename std::enable_if<std::is_same<U, int>::value, void>:: type testFunction(T a, U b) { std::cout << "b is integer\n"; } template <typename T, typename U> typename std::enable_if<std::is_same<U, float>::value, void>:: type testFunction(T a, U b) { std::cout << "b is float\n"; }

但是,类似的可变参数模板不能编译:

template <typename T, typename... U> typename std::enable_if<std::is_same<U, int>::value, void>:: type testFunction(T a, U... bs) { std::cout << "bs are integers\n"; } template <typename T, typename... U> typename std::enable_if<std::is_same<U, float>::value, void>:: type testFunction(T a, U... bs) { std::cout << "bs are floats\n"; }

也许我正在尝试做一些无法完成的事情。 我知道使用初始化器列表可以实现类似的功能,但是我想避免初始化器列表参数所需的大括号。

These two non-variadic function templates do compile:

template <typename T, typename U> typename std::enable_if<std::is_same<U, int>::value, void>:: type testFunction(T a, U b) { std::cout << "b is integer\n"; } template <typename T, typename U> typename std::enable_if<std::is_same<U, float>::value, void>:: type testFunction(T a, U b) { std::cout << "b is float\n"; }

however, similar variadic templates do not compile:

template <typename T, typename... U> typename std::enable_if<std::is_same<U, int>::value, void>:: type testFunction(T a, U... bs) { std::cout << "bs are integers\n"; } template <typename T, typename... U> typename std::enable_if<std::is_same<U, float>::value, void>:: type testFunction(T a, U... bs) { std::cout << "bs are floats\n"; }

Maybe I am trying to do something that cannot be done. I know that similar functionality can be achieved using initializer lists, but I would like to avoid curly brackets required for initializer list arguments.

最满意答案

是。 你可以在C ++ 17中使用fold表达式

template <typename T, typename... U> typename std::enable_if<(std::is_same<U, float>::value && ...), void>:: type testFunction(T a, U... bs) { std::cout << "bs are floats\n"; }

在C ++ 11中,你可以重新实现std::conjunction :

template<class...> struct conjunction : std::true_type { }; template<class B1> struct conjunction<B1> : B1 { }; template<class B1, class... Bn> struct conjunction<B1, Bn...> : std::conditional_t<bool(B1::value), conjunction<Bn...>, B1> {};

template <typename T, typename... U> typename std::enable_if< std::conjunction_v<std::is_same<U, float>::value...>, void >::type testFunction(T a, U... bs) { std::cout << "bs are floats\n"; }

Yes. You can use a fold expression in C++17:

template <typename T, typename... U> typename std::enable_if<(std::is_same<U, float>::value && ...), void>:: type testFunction(T a, U... bs) { std::cout << "bs are floats\n"; }

In C++11, you can reimplement std::conjunction:

template<class...> struct conjunction : std::true_type { }; template<class B1> struct conjunction<B1> : B1 { }; template<class B1, class... Bn> struct conjunction<B1, Bn...> : std::conditional_t<bool(B1::value), conjunction<Bn...>, B1> {};

template <typename T, typename... U> typename std::enable_if< std::conjunction_v<std::is_same<U, float>...>, void >::type testFunction(T a, U... bs) { std::cout << "bs are floats\n"; }

更多推荐

本文发布于:2023-08-04 18:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1420902.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:enable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!