此代码:
double foo(double opd1, double opd2) {
return opd1 + opd2;
}
#include <functional>
#include <stack>
#include <type_traits>
class E {
public:
struct F {
F(const std::type_info& _type) : type(_type) {}
virtual ~F() {}
const std::type_info& type;
virtual const void* f() = 0;
virtual void d() = 0;
};
public:
// function
template <typename R, typename... T>
void functionN(typename std::enable_if<sizeof...(T) == 2, R (*)(T...)>::type f) {
std::pair<void (*)(), void (*)(void (*)(), std::stack<F*>&)> m_f = std::make_pair(reinterpret_cast<void (*)()>(f), f2compile<R, T...>);
}
template<typename R, typename T1, typename T2> void static f2compile(void (*f)(), std::stack<F*>& s) {
auto opd2 = s.top();
s.pop();
auto opd1 = s.top();
s.pop();
s.push(new F2<R,T1,T2>(f, opd1, opd2));
}
private:
template <typename R, typename T1, typename T2> struct F2 : F {
F2(void (*_fn)(), F* _opd1, F* _opd2) : F(typeid(R))
, fn(reinterpret_cast<R(*)(T1,T2)>(_fn))
, opd1(_opd1)
, opd2(_opd2)
{}
const void* f() { res = fn(*(T1*) opd1->f(), *(T2*) opd2->f()); return &res; }
F* opd1; F* opd2;
R res; R (*fn)(T1,T2);
void d() { opd1->d(); opd2->d(); delete this; }
};
};
TEST_CASE("expression") {
E e;
e.functionN<double,double,double>(foo);
}
编译工作。
我想写没有静态成员的东西
void f2compile
但是我收到编译器错误
/usr/bin/g++ -g -pthread -std=c++11 -o /home/mariorollino/SVN/PTL2/bin/c++11 \
/home/mariorollino/SVN/PTL2/src/c++11/c++11.cc
/home/mariorollino/SVN/PTL2/src/c++11/c++11.cc: In instantiation of 'void E::functionN(typename std::enable_if<(sizeof (T ...) == 2), R (*)(T ...)>::type) [with R = double; T = {double, double}; typename std::enable_if<(sizeof (T ...) == 2), R (*)(T ...)>::type = double (*)(double, double)]':
/home/mariorollino/SVN/PTL2/src/c++11/c++11.cc:472:40: required from here
/home/mariorollino/SVN/PTL2/src/c++11/c++11.cc:430:137: error: invalid initialization of reference of type 'void (E::*&&)(void (*)(), std::stack<E::F*>&)' from expression of type '<unresolved overloaded function type>'
std::pair<void (*)(), void (*)(void (*)(), std::stack<F*>&)> m_f = std::make_pair(reinterpret_cast<void (*)()>(f), f2compile<R, T...>);
^
In file included from /usr/include/c++/4.8.2/bits/stl_algobase.h:64:0,
from /usr/include/c++/4.8.2/bits/char_traits.h:39,
from /usr/include/c++/4.8.2/ios:40,
from /usr/include/c++/4.8.2/istream:38,
from /usr/include/c++/4.8.2/sstream:38,
from /home/mariorollino/SVN/PTL2/src/c++11/../ptl/catch.hpp:377,
from /home/mariorollino/SVN/PTL2/src/c++11/c++11.cc:2:
/usr/include/c++/4.8.2/bits/stl_pair.h:276:5: error: in passing argument 2 of 'constexpr std::pair<typename std::__decay_and_strip<_Tp>::__type, typename std::__decay_and_strip<_T2>::__type> std::make_pair(_T1&&, _T2&&) [with _T1 = void (*)(); _T2 = void (E::*)(void (*)(), std::stack<E::F*>&); typename std::__decay_and_strip<_T2>::__type = void (E::*)(void (*)(), std::stack<E::F*>&); typename std::__decay_and_strip<_Tp>::__type = void (*)()]'
make_pair(_T1&& __x, _T2&& __y)
^
我探查声明一个指向类似成员函数的指针
std::pair<void (*)(), void (E::*)(void (*)(), std::stack<F*>&)> m_f = std::make_pair(reinterpret_cast<void (*)()>(f), f2compile<R, T...>);
但不起作用。
我探讨了auto的使用
auto t = f2compile;
这项工作,但我不知道t的类型。
帮我...谢谢。