如何从模板函子包装器返回任意类型(void或non-void)?我使用包装器用于前置和后置条件,因此我需要在从包装器返回之前将返回值存储在局部变量中。但是当返回的类型为void时,编译器会给出错误,因为变量不能具有void类型。可以在这做什么?
template <typename Functor, typename... Args>
auto Decorate(Functor f, Args&&... args)
-> decltype(f(std::forward<Args>(args)...)) {
// preconditions
const auto result = f(std::forward<Args>(args)...);
// postconditions
return result;
}
答案 0 :(得分:5)
在合适类的构造函数/析构函数中运行前置条件和后置条件,并直接返回值!只要您不需要触摸后期条件中的返回值,这应该不是问题!
struct condition
{
condition() { /* do pre-condition checks */ }
~condition() { /* do post-condition checks */ }
condition(condition&) = delete;
void operator= (condition&) = delete;
};
template <typename Functor, typename... Args>
auto Decorate(Functor f, Args&&... args)
-> decltype(f(std::forward<Args>(args)...)) {
condition checker;
return f(std::forward<Args>(args)...);
}
答案 1 :(得分:1)
使用sfinae:
#include <type_traits>
template <typename Functor, typename... Args>
auto Decorate(Functor f, Args&&... args)
-> typename std::enable_if<std::is_same<decltype(f(std::forward<Args>(args)...)), void>::value, void>::type
{
// preconditions
f(std::forward<Args>(args)...);
// postconditions
return;
}
template <typename Functor, typename... Args>
auto Decorate(Functor f, Args&&... args)
-> typename std::enable_if<!std::is_same<decltype(f(std::forward<Args>(args)...)), void>::value, decltype(f(std::forward<Args>(args)...))>::type
{
// preconditions
auto result = f(std::forward<Args>(args)...);
// postconditions
return result;
}
试验:
void f(int x) {
std::cout << "f(" << x << ")" << std::endl;
return;
}
int g(int x) {
std::cout << "g(" << x << ")" << std::endl;
return x * x;
}
int main()
{
Decorate(f, 3);
std::cout << Decorate(g, 3);
return 0;
}
F(3)
克(3)
9个