我正在围绕std :: function创建一个模板包装器。为了匹配void返回函数,我将std :: monostate作为我的Unit类型。我一直在尝试为我的函数包装器类型创建两个可变参数模板,一个
template<Unit, Args...>
和一个
template<T, Args...> where T != Unit.
这是我使用enable_if
时最接近的。
#include <functional>
#include <variant>
namespace func {
// Declare unit type
typedef std::monostate Unit;
// Empty type, maybe use this instead of monostate
template<typename ... ts>
struct Term {};
// Variables
template<typename T>
struct Term<T>
{
T val;
// Constructor
Term(T in): val(in) {}
// Call op just returns the value
T operator() () const {
return this->val;
}
};
// Functions that return void
template <typename T,
typename ... Args,
typename = std::enable_if_t<std::is_same<T, Unit>::value>>
struct Term<Args...>
{
// Void returning function
const std::function<void(Args...)> body;
// Void returning constructor
Term(std::function<void(Args...)> func): body(func) {}
// Void function Caller
void operator() (const Args&& ...a) const {
this->body(std::forward<Args>(a)...);
}
};
// Functions that return T
template <typename T,
typename ... Args,
typename = std::enable_if_t<!std::is_same<T, Unit>::value>>
struct Term<T, Args...>
{
// T returning function
const std::function<T(Args...)> body;
// T returning constructor
Term(std::function<T(Args...)> func): body(func) {}
// T returning function Caller
T operator() (const Args&& ...a) const {
return this->body(std::forward<Args>(a)...);
}
};
}
然而,我在第一种情况下得到关于不可推导参数T的错误。但是,我已经知道我的Unit
模板参数的参数类型为enable_if
。如何让编译器接受这两个定义?
错误:
$ clang++ -std=c++17 terms.hpp -pedantic
terms.hpp:29:18: error: default template argument in a class template partial specialization
typename = std::enable_if_t<std::is_same<T, Unit>::value>>
^
terms.hpp:30:9: error: class template partial specialization contains template parameters that cannot be deduced; this partial specialization will never be used [-Wunusable-partial-specialization]
struct Term<Args...>
^~~~~~~~~~~~~
terms.hpp:27:21: note: non-deducible template parameter 'T'
template <typename T,
^
terms.hpp:29:7: note: non-deducible template parameter (anonymous)
typename = std::enable_if_t<std::is_same<T, Unit>::value>>
^
terms.hpp:47:18: error: default template argument in a class template partial specialization
typename = std::enable_if_t<!std::is_same<T, Unit>::value>>
^
terms.hpp:48:9: error: class template partial specialization contains a template parameter that cannot be deduced; this partial specialization will never be used [-Wunusable-partial-specialization]
struct Term<T, Args...>
^~~~~~~~~~~~~~~~
terms.hpp:47:7: note: non-deducible template parameter (anonymous)
typename = std::enable_if_t<!std::is_same<T, Unit>::value>>
^
4 errors generated.
编辑:应该像这样使用
auto c = Term<int>(42);
auto fun = Term<int, int, int>([](int a, int b) { return a + b; });
std::cout << c() << std::endl;
std::cout << fun(3,4) << std::endl;
答案 0 :(得分:1)
您目前在模板特化方面遇到的问题是,struct Term<Args...>
和struct Term<T, Args...>
不是互斥的,可以匹配相同的内容。因此,我建议您将这两个案例统一为一个,并将SFINAE统一到重载的呼叫运算符上。
这种方法当然有一些局限性,但这些只是继承了原来的方法。例如,不可能包含一个不带参数但返回值的函数(这与标量不一致)。
#include <functional>
#include <iostream>
#include <variant>
namespace func {
// Declare unit type
typedef std::monostate Unit;
// Empty type, maybe use this instead of monostate
template<typename...>
struct Term;
// Variables
template<typename T>
struct Term<T>
{
T val;
// Constructor
Term(T in): val(in) {}
// Call op just returns the value
T operator() () const {
return this->val;
}
};
// Functions that return void
template <typename T, typename ... Args>
struct Term<T, Args...>
{
using R = typename std::conditional<std::is_same<T, Unit>::value,void,T>::type;
// Void returning function
const std::function<R(Args...)> body;
// Void returning constructor
Term(std::function<R(Args...)> func): body(func) {}
// Void function Caller
template <typename U = R>
typename std::enable_if<std::is_same<U, Unit>::value,void>::type
operator() (Args&& ...a) const {
this->body(std::forward<Args>(a)...);
}
// T returning function Caller
template <typename U = R>
typename std::enable_if<!std::is_same<U, Unit>::value,T>::type
operator() (Args&& ...a) const {
return this->body(std::forward<Args>(a)...);
}
};
}
int main() {
auto c = func::Term<int>(42);
auto fun = func::Term<int, int, int>([](int a, int b) { return a + b; });
std::cout << c() << std::endl;
std::cout << fun(3,4) << std::endl;
}