Tre*_*key 13 c++ type-traits enable-if template-meta-programming c++14
C++ 14将具有可以根据返回值推断出返回类型的函数.
auto function(){
return "hello world";
}
Run Code Online (Sandbox Code Playgroud)
我可以将此行为应用于通过返回类型惯用法对SFINAE 使用enable_if的函数吗?
例如,让我们考虑以下两个功能:
#include <type_traits>
#include <iostream>
//This function is chosen when an integral type is passed in
template<class T >
auto function(T t) -> typename std::enable_if<std::is_integral<T>::value>::type {
std::cout << "integral" << std::endl;
return;
}
//This function is chosen when a floating point type is passed in
template<class T >
auto function(T t) -> typename std::enable_if<std::is_floating_point<T>::value>::type{
std::cout << "floating" << std::endl;
return;
}
int main(){
function(1); //prints "integral"
function(3.14); //prints "floating"
}
Run Code Online (Sandbox Code Playgroud)
如您所见,使用SFINAE按返回类型惯用法选择正确的函数.但是,这些都是无效功能.第二个参数enable_if默认设置为void.这将是相同的:
//This function is chosen when an integral type is passed in
template<class T >
auto function(T t) -> typename std::enable_if<std::is_integral<T>::value, void>::type {
std::cout << "integral" << std::endl;
return;
}
//This function is chosen when a floating point type is passed in
template<class T >
auto function(T t) -> typename std::enable_if<std::is_floating_point<T>::value, void>::type{
std::cout << "floating" << std::endl;
return;
}
Run Code Online (Sandbox Code Playgroud)
我可以对这两个函数做些什么,以便它们的返回类型由返回值推导出来?
gcc 4.8.2(使用--std=c++1y)
Rap*_*ptz 12
std::enable_if 不必是返回类型,从C++ 11开始,它可以是模板参数的一部分.
所以你的等效函数可以(或者,好的,这个效果):
enum class enabler_t {};
template<typename T>
using EnableIf = typename std::enable_if<T::value, enabler_t>::type;
//This function is chosen when an integral type is passed in
template<class T, EnableIf<std::is_integral<T>>...>
auto function(T t) {
std::cout << "integral" << std::endl;
return;
}
//This function is chosen when a floating point type is passed in
template<class T, EnableIf<std::is_floating_point<T>>...>
auto function(T t) {
std::cout << "floating" << std::endl;
return;
}
Run Code Online (Sandbox Code Playgroud)
它也可以是函数中的参数:
//This function is chosen when an integral type is passed in
template<class T>
auto function(T t, EnableIf<std::is_integral<T>>* = nullptr) {
std::cout << "integral" << std::endl;
return;
}
//This function is chosen when a floating point type is passed in
template<class T>
auto function(T t, EnableIf<std::is_floating_point<T>>* = nullptr) {
std::cout << "floating" << std::endl;
return;
}
Run Code Online (Sandbox Code Playgroud)
这将保留自动类型扣除和SFINAE.
小智 6
std::enable_if可以是返回类型,函数参数或模板参数.如果使用返回类型或模板参数,则会出现函数重定义错误,因此需要将其std::enable_if用作函数参数:
#include <type_traits>
#include <iostream>
template<class T, typename = typename std::enable_if<std::is_integral<T>::value, void>::type>
auto function(T t, typename std::enable_if<std::is_integral<T>::value, void>::type* dummy = nullptr) {
std::cout << "integral" << std::endl;
return 0;
}
//This function is chosen when a floating point type is passed in
template<class T, typename = typename std::enable_if<std::is_floating_point<T>::value, void>::type>
auto function(T t, typename std::enable_if<std::is_floating_point<T>::value, void>::type* dummy = nullptr) {
std::cout << "floating" << std::endl;
return 0.0f;
}
int main()
{
auto ret = function(0); // integral
auto ret2 = function(0.0f); // floating
std::cout << std::boolalpha << std::is_integral<decltype(ret)>::value << std::endl; // true
std::cout << std::is_floating_point<decltype(ret2)>::value << std::endl; // true
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6377 次 |
| 最近记录: |