C++ 空函数(…)和空类型是否相同?

C++ 空函数(…)和空类型是否相同?,c++,typetraits,std-function,C++,Typetraits,Std Function,我得到了一段代码: template <class FunctionType> class Entry { std::function<FunctionType> internalFunction; template<class... Arguments> auto operator()(Arguments... arguments) -> decltype(internalFunction(arguments...)){

我得到了一段代码:

template <class FunctionType> class Entry {
    std::function<FunctionType> internalFunction;

    template<class... Arguments>
    auto operator()(Arguments... arguments) -> decltype(internalFunction(arguments...)){

        if (std::is_same<decltype(internalFunction(arguments...)), void>::value) {
            internalFunction(arguments...);
        } else {
            auto result = internalFunction(arguments...);

            return result;      
        }
    }
};
模板类条目{
std::函数内部函数;
模板
自动运算符()(参数…参数)->decltype(内部函数(参数…){
if(std::is_same::value){
内部函数(参数…);
}否则{
自动结果=内部函数(参数…);
返回结果;
}
}
};
Entry类是
std::function
的包装器。它适用于所有返回类型,只有一个例外-
void
。我不能让它工作。我还尝试了
std::is_void
,对于
void(…)
类型的函数,它不会返回true。std::的用法相同

如何解决这个问题

return internalFunction(arguments...);

即使
internalFunction
返回
void

尝试将结果存储在中间对象中不起作用,因为您无法创建类型为
void
的对象,因为它不是对象类型

<> > <代码>如果不工作,因为<代码>如果是运行时条件,编译器仍然需要编译条件的两个分支,因此它们必须都是有效的C++ +< /P> 如果需要创建中间结果类型的变量,则不能将该代码用于
void
案例。您可以为返回的函数编写部分专门化
void

template <class FunctionType> class Entry {
    std::function<FunctionType> internalFunction;

    template<class... Arguments>
    auto operator()(Arguments... arguments) -> decltype(internalFunction(arguments...))
    {

        auto result = internalFunction(arguments...);

        return result;      
    }
};

template <class... ArgTypes> class Entry<void(ArgTypes...)> {
    std::function<void(ArgTypes...)> internalFunction;

    template<class... Arguments>
    void operator()(Arguments... arguments) {
        internalFunction(arguments...);
    }
}; 
模板类条目{
std::函数内部函数;
模板
自动运算符()(参数…参数)->decltype(内部函数(参数…)
{
自动结果=内部函数(参数…);
返回结果;
}
};
模板类条目{
std::函数内部函数;
模板
void运算符()(参数…参数){
内部函数(参数…);
}
}; 

这对返回
void
的函数不起作用,但对返回
void
的函子不起作用,这样做有点困难。

它遵循另一种解决方案,即基于sfinae而不是部分专门化的解决方案。
我试图提供一个最小、完整的示例。
我还想在示例中引入完美转发,但它与问题中的完全不同,所以我决定让它更类似于那个

#include<functional>
#include<type_traits>


template <class FunctionType> class Entry {
    std::function<FunctionType> internalFunction;

    template<typename R, typename... Args>
    typename std::enable_if<std::is_void<R>::value>::type
    invoke(Args... args) {
        internalFunction(args...);
    }

    template<typename R, typename... Args>
    typename std::enable_if<not std::is_void<R>::value, R>::type
    invoke(Args... args) {
        return internalFunction(args...);
    }

public:
    Entry(std::function<FunctionType> f)
        : internalFunction{f} { }

    template<class... Arguments>
    auto operator()(Arguments... arguments) -> decltype(internalFunction(arguments...)){
        return invoke<typename std::function<FunctionType>::result_type>(arguments...);
    }
};


int f() { return 42; }
void g() { }


int main() {
    Entry<int()> e1(&f);
    e1();
    Entry<void()> e2(&g);
    e2();
}
#包括
#包括
模板类条目{
std::函数内部函数;
模板
typename std::enable_if::type
调用(Args…Args){
内部功能(args…);
}
模板
typename std::enable_if::type
调用(Args…Args){
返回内部函数(args…);
}
公众:
条目(std::函数f)
:内部函数{f}{}
模板
自动运算符()(参数…参数)->decltype(内部函数(参数…){
返回调用(参数…);
}
};
int f(){return 42;}
void g(){}
int main(){
条目e1&f;
e1();
条目e2&g;
e2();
}

有关sfinae的更多详细信息,请参阅。

internalFunction
返回
std::function
。我搞不懂你想检查什么
internalFunction
将永远不会返回
void
。我必须存储结果,它不是全部代码。然后您需要专门化模板并有两个定义,一个用于
void
返回,另一个用于其他所有内容。@Nuurek会让事情变得非常糟糕-区分返回void和非void的函数我也尝试过。编译器说函数调用不明确。这就是我需要的。谢谢:)