问题
I've got the following code:
#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;
}
When I compile I've got the following error:
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
I'm trying to apply SFINAE in order to have the push method applied according to the type used. How to solve?
回答1:
Default argument is not part of the template parameter list, thus your two push
have identical template parameter list, which leads a redeclaration.
This is a typical drawback of typename = std::enable_if_t<...>
approach. Instead, you should use std::enable_if_t<..., int> = 0
approach.
Change to this:
#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
来源:https://stackoverflow.com/questions/53351945/applying-sfinae-pattern-with-universal-reference