高分求C++ boost库 BOOST_AUTO宏的原理,在vs2008环境,boost库(忘了什么版本了)#include using namespace std;int test(){static int a = 0;++ a;return a;}int main(){BOOST_AUTO(b,test());int a = 0;++ a;//test();}宏展开是boost::type_o
来源:学生作业帮助网 编辑:作业帮 时间:2024/06/23 21:36:48
![高分求C++ boost库 BOOST_AUTO宏的原理,在vs2008环境,boost库(忘了什么版本了)#include using namespace std;int test(){static int a = 0;++ a;return a;}int main(){BOOST_AUTO(b,test());int a = 0;++ a;//test();}宏展开是boost::type_o](/uploads/image/z/11075556-12-6.jpg?t=%E9%AB%98%E5%88%86%E6%B1%82C%2B%2B+boost%E5%BA%93+BOOST_AUTO%E5%AE%8F%E7%9A%84%E5%8E%9F%E7%90%86%2C%E5%9C%A8vs2008%E7%8E%AF%E5%A2%83%2Cboost%E5%BA%93%EF%BC%88%E5%BF%98%E4%BA%86%E4%BB%80%E4%B9%88%E7%89%88%E6%9C%AC%E4%BA%86%EF%BC%89%23include+using+namespace+std%3Bint+test%28%29%7Bstatic+int+a+%3D+0%3B%2B%2B+a%3Breturn+a%3B%7Dint+main%28%29%7BBOOST_AUTO%28b%2Ctest%28%29%29%3Bint+a+%3D+0%3B%2B%2B+a%3B%2F%2Ftest%28%29%3B%7D%E5%AE%8F%E5%B1%95%E5%BC%80%E6%98%AFboost%3A%3Atype_o)
高分求C++ boost库 BOOST_AUTO宏的原理,在vs2008环境,boost库(忘了什么版本了)#include using namespace std;int test(){static int a = 0;++ a;return a;}int main(){BOOST_AUTO(b,test());int a = 0;++ a;//test();}宏展开是boost::type_o
高分求C++ boost库 BOOST_AUTO宏的原理,
在vs2008环境,boost库(忘了什么版本了)
#include
using namespace std;
int test()
{
static int a = 0;
++ a;
return a;
}
int main()
{
BOOST_AUTO(b,test());
int a = 0;
++ a;
//test();
}
宏展开是boost::type_of::msvc_typeid_wrapper::type b = test();
编译结果只有两句话
0041140E call test (41107Dh)
00411413 mov dword ptr [b],eax
跟 int b = test();是一样的.
那么,boost::type_of::msvc_typeid_wrapper::type是怎么变成int的呢
高分求C++ boost库 BOOST_AUTO宏的原理,在vs2008环境,boost库(忘了什么版本了)#include using namespace std;int test(){static int a = 0;++ a;return a;}int main(){BOOST_AUTO(b,test());int a = 0;++ a;//test();}宏展开是boost::type_o
主要是利用模板片特化,
模板参数不仅仅可以实现还能是整形数
你看这个
template
struct msvc_typeid_wrapper {
typedef msvc_typeid_wrapper type;
};
它实际上就是一个片特化,如果模板参数是1(这个1往往又利用枚举的值来传递),至于变成int,主要是是下面的宏与条件编译联合作用的结果
# define BOOST_TYPEOF_NESTED_TYPEDEF_TPL(name,expr) \
struct name {\
BOOST_STATIC_CONSTANT(int,_typeof_register_value=sizeof(boost::type_of::typeof_register_type(expr)));\
typedef typename boost::type_of::msvc_extract_type::id2type id2type;\
typedef typename id2type::type type;\
};