本帖最后由 dong_abc 于 2013-8-31 12:20 编辑
2、C++11预定义宏
2.1 _func_ ,在C99中支持_func_ 返回所在函数名的功能。在C++11标准中允许在类/结构体中使用。
- class A
- {
- public:
- A():name(_func_) {};
- const char *name ;
- };
2.2 _Pragma
在之前的标准中,我们都是用一下方式来避免头文件重复编译
- #ifndef __NUC1xxRtc_H__
- #define __NUC1xxRtc_H__
- ...
- ...
- #endif
C++11简化了这一点,用_Pragma(“字符串”)即可
2.3 静态断言
我们之前都是用assert(...)动态断言来处理,这种方法需要程序运行时才能处理,运行程序需要一些额外的开销,
C++11推出了一种静态断言static_assert,在编译时就能提前处理,不需要运行开销。- #include <iostream>
- using namespace std;
- template <typename T1, typename T2>
- auto add(T1 t1, T2 t2) -> decltype(t1 + t2)
- {
- static_assert(std::is_integral<T1>::value, "Type T1 must be integral");
- static_assert(std::is_integral<T2>::value, "Type T2 must be integral");
- return t1 + t2;
- }
- int main()
- {
- //std::cout << add(1, 3.14) << std::endl;
- std::cout << add(111, 2) << std::endl;
- return 0;
- }
C++11中还更新了一些其他 宏机制,用得较少,就不关注了。
|