将SFINAE模式与通用参考一起应用

编程入门 行业动态 更新时间:2024-10-12 05:46:02
本文介绍了将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模式与通用参考一起应用

本文发布于:2023-11-17 09:11:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609576.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模式   SFINAE

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!