从A引用
Boost mailing list
Arkadiy Vertleyb的文章:
上帝只知道还能找到什么
如果愿意的话,在这个编译器内部
挖得很深。
例如,来自RSDN的igor'chesnokov
(俄罗斯软件开发网络)
已找到实现typeof()的方法
不需要注册,
可能还有编译时间
本机类型的性能。
怎样?显然,一些奇怪的“特征”
VisualC++让他变
模板主体
实例化,当附加上下文
可用,因此“注册”
就在这一刻,
正在获取typeof()。
微软特定的“bugfeatures”是
一般不在我的区域
利益。但是我知道,
在Microsoft编译器上,这可能
那么看起来更有吸引力
我和佩德实施过的任何事情。
即使我意识到
可能是个严重的竞争,我
会觉得很不舒服,更不用说了
这里是:
http://rsdn.ru/Forum/?mid=1094305
并引用上述引用链接引用的页面中的igor chesnokov的代码:
// type_of() evil implementation for VC7
//
// (c) Chez
// mailto:chezu@pisem.net
#include "stdafx.h"
// This file contains:
// 1) type_id(type)
// 2) var_type_id(expersssion)
// 3) type_of(expression)
// IMPLEMENTATION
template<int ID>
class CTypeRegRoot
{
public:
class id2type;
};
template<typename T, int ID>
class CTypeReg : public CTypeRegRoot<ID>
{
public:
class CTypeRegRoot<ID>::id2type // This uses nice VC6-VC7 bugfeature
{
public:
typedef T Type;
};
typedef void Dummy;
};
template<int N>
class CCounter;
// TUnused is required to force compiler to recompile CCountOf class
template<typename TUnused, int NTested = 0>
class CCountOf
{
public:
enum
{
__if_exists(CCounter<NTested>) { count = CCountOf<TUnused, NTested + 1>::count }
__if_not_exists(CCounter<NTested>) { count = NTested }
};
};
template<class TTypeReg, class TUnused, int NValue> // Helper class
class CProvideCounterValue
{
public:
enum { value = NValue };
};
// type_id
#define type_id(type) \
(CProvideCounterValue< \
/*register TYPE--ID*/ typename CTypeReg<type, CCountOf<type >::count>::Dummy, \
/*increment compile-time Counter*/ CCounter<CCountOf<type >::count>, \
/*pass value of Counter*/CCountOf<type >::count \
>::value)
// Lets type_id() be > than 0
class __Increment_type_id { enum { value = type_id(__Increment_type_id) }; };
template<int NSize>
class sized
{
private:
char m_pad[NSize];
};
template<typename T>
typename sized<type_id(T)> VarTypeID(T&);
template<typename T>
typename sized<type_id(const T)> VarTypeID(const T&);
template<typename T>
typename sized<type_id(volatile T)> VarTypeID(volatile T&);
template<typename T>
typename sized<type_id(const volatile T)> VarTypeID(const volatile T&);
// Unfortunatelly, var_type_id() does not recognize references
#define var_type_id(var) \
(sizeof(VarTypeID(var)))
// type_of
#define type_of(expression) \
/* This uses nice VC6-VC7 bugfeature */ \
CTypeRegRoot<var_type_id(expression)>::id2type::Type
// auto_operator
#define auto_operator(arg1, arg2, op) \
type_of(instance(arg1) op instance(arg2)) operator op
// TEST
class A
{
public:
friend static const char* operator +(const A& a, const A& b)
{
return "chijik-pijik";
}
};
template<typename T>
class Plus
{
public:
friend static type_of(T() + T()) operator +(const Plus<T>& a, const Plus<T>& b)
{
return a.m + b.m;
}
T m;
};
int _tmain(int argc, _TCHAR* argv[])
{
Plus<A> a1, a2;
const char* x = a1 + a2;
return 0;
}
现在我还没有尝试过这段代码,而且,由于它使用了编译器特有的东西,请注意它是为MSVC 7.x编写的,所以它可能或不适用于更高版本。希望如此?
干杯!