【发布时间】:2015-01-12 18:55:06
【问题描述】:
我有一个宏:
#define BIND(c_slot, cxx_target) c_slot = & Generate<
decltype(c_slot)
, decltype(&cxx_target)
, &cxx_target
>::call;
我会这样使用它:
BIND( table->fooslot , Base::foofunc );
Generate 看起来像这样:
template<typename Fc, typename Target, Target target>
struct Generate;
template < typename R , typename ...Arg ,
typename RTarg , typename ...TargArg ,
RTarg(ExtObjBase_noTemplate::*target)(TargArg...) >
struct Generate< R(*)(PyObject*, Arg...), RTarg(ExtObjBase_noTemplate::* )(TargArg...), target >
{
static R call( PyObject* self, Arg... carg)
{
std::cout << "SLOT!" << std::endl;
try
{
RTarg r_cxx = (cxxbase_for(self)->*target) (Convert<Arg>::to_cxx(carg) ...);
return Convert<RTarg>::to_c(r_cxx);
}
catch (...)
{
:
}
}
};
我想改进 std::cout 以便它输出 WHICH 插槽。
类似:
#define BIND(c_slot, cxx_target) c_slot = & Generate<
decltype(c_slot)
, decltype(&cxx_target)
, &cxx_target
, #c_slot
>::call;
但我不知道如何使它工作。
有没有办法做到这一点?
编辑:一种可能的方法是:
static std::map<void*, std::string> names_map;
:
#define BIND(c_slot, cxx_target) \
c_slot = & Generate< decltype(c_slot) ,decltype(&cxx_target), &cxx_target >::call; \
names_map[offset_of(&cxx_target)] = std::string(#c_slot);
:
template < STUFF >
struct Generate< STUFF >
{
static R call( STUFF )
{
COUT( "SLOT: " << names_map[offset_of(???)] );
...有什么办法让它工作吗?
编辑:这个问题已经解决了here
【问题讨论】:
-
字符串字面量不能作为模板参数,因此您必须使用一些二进制递归宏将其拆分为合理数量的字符。