如何将std :: enable_if与自推断返回类型一起使用? [英] How do I use std::enable_if with a self-deducing return type?
问题描述
C ++ 14 将具有其推导类型可以推断出的函数根据返回值。
C++14 will have functions whose return type can be deduced based on the return value.
auto function(){
return "hello world";
}
我可以将此行为应用于使用 enable_if 通过返回类型成语用于SFINAE吗?
Can I apply this behaviour to functions that use enable_if for the SFINAE by return type idiom?
例如,让我们考虑以下两个功能:
For example, let's consider the following two functons:
#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"
}
如您所见,使用SFINAE通过返回类型惯用法选择正确的函数。
但是,这两个都是无效函数。 enable_if
的第二个参数默认设置为 void
。将会是相同的:
As you can see, the correct function is chosen using the SFINAE by return type idiom.
However, these are both void functions. The second parameter of enable_if
is default set to void
. This would be the same:
//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;
}
我可以对这两个函数执行某些操作,以便它们的返回类型是由返回值推导出来的?
Is there something I can do to these two functions, so that their return type is deduced by the return value?
gcc 4.8.2(使用-std = c ++ 1y
)
gcc 4.8.2 (using --std=c++1y
)
推荐答案
std :: enable_if
不必位于返回类型,从C ++ 11开始,它可以是模板参数的一部分。
std::enable_if
doesn't have to be in the return type, as of C++11 it can be part of the template parameters.
因此,您的等效函数可以是(或者,可以达到这种效果):
So your equivalent functions can be (or, well, something to this effect):
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;
}
它也可以是函数中的参数:
It can also be a parameter in the function:
//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;
}
这将保留自动类型推断和SFINAE。
This will keep the automatic type deduction and SFINAE.
这篇关于如何将std :: enable_if与自推断返回类型一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!