将SFINAE模式与通用参考一起应用 [英] Applying SFINAE pattern with universal reference

查看:66
本文介绍了将SFINAE模式与通用参考一起应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

#include <utility>
#include <iostream>
#include <queue>
#include <functional>
#include <stdio.h>
#include <future>

class Runner {
public:
    virtual void run() = 0;
    virtual ~Runner() {
    }
};

class Command: public Runner {
public:
    Command() {
        std::cout << "constructor" << std::endl;
    }
    virtual void run() {
    }
};

#define EXTEND(T, F) , typename = typename std::enable_if<std::is_base_of<F, typename std::decay<T>::type>::value, typename std::decay<T>::type>::type
#define NOT_EXTEND(T, F) , typename = typename std::enable_if<!std::is_base_of<F, typename std::decay<T>::type>::value, typename std::decay<T>::type>::type

class Executor {
private:
    std::queue<std::function<void(void)> > q;
public:
    template<class T EXTEND(T, Runner)>
    void push(T&& toBepushed) {
        q.push(
                std::bind(&std::decay<T>::type::run,
                        std::forward<T>(toBepushed)));
    }
    template<typename T NOT_EXTEND(T, Runner)>
    void push(T&& toBepushed) {
        q.push(std::forward<T>(toBepushed));
    }
    void perform() {
        std::function<void(void)>&& f = std::move(q.front());
        f();
    }
};

int main() {
    Executor b;
    Command c;
    b.push(c);
    b.perform();
    return 0;
}

编译时出现以下错误:


g ++ -std = c ++ 0x -O2 -g3 -Wall -c -fmessage-length = 0 -MMD -MP
-MF main.d -MT main.o -o main.o ../main.cpp ../main.cpp:45:7:错误:'template void Bar :: push(T& ;)'不能被
重载void push(T& toBepushed){
^ ~~~ ../main.cpp:39:7:error:with'template void Bar :: push( T&)'void push(T& toBepushed){
^ ~~~ make:*** [subdir.mk:20:main.o]错误1

g++ -std=c++0x -O2 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.o" -o "main.o" "../main.cpp" ../main.cpp:45:7: error: ‘template void Bar::push(T&&)’ cannot be overloaded void push(T&& toBepushed) { ^~~~ ../main.cpp:39:7: error: with ‘template void Bar::push(T&&)’ void push(T&& toBepushed) { ^~~~ make: *** [subdir.mk:20: main.o] Error 1

我正在尝试应用SFINAE,以便根据所使用的类型应用push方法。

I'm trying to apply SFINAE in order to have the push method applied according to the type used. How to solve?

推荐答案

默认参数不属于模板参数列表,因此,您的两个 push 具有相同的模板参数列表,这导致重新声明。

Default argument is not part of the template parameter list, thus your two push have identical template parameter list, which leads a redeclaration.

这是 typename = std ::的典型缺陷: enable_if_t< ...> 方法。相反,您应该使用 std :: enable_if_t< ...,int> = 0 方法。

This is a typical drawback of typename = std::enable_if_t<...> approach. Instead, you should use std::enable_if_t<..., int> = 0 approach.

更改为此:

#define EXTEND(T, F) , typename std::enable_if<std::is_base_of<F, typename std::decay<T>::type>::value, int>::type = 0
#define NOT_EXTEND(T, F) , typename std::enable_if<!std::is_base_of<F, typename std::decay<T>::type>::value, int>::type = 0

这篇关于将SFINAE模式与通用参考一起应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆