首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++最反直觉的地方,该怎么处理

2013-01-06 
C++最反直觉的地方1 auto 会自动把 引用 去除掉int& get()auto k get()// k的类型是int,而不是int&Der

C++最反直觉的地方

1 auto 会自动把 引用 去除掉
  
  int& get(); 
  auto k = get();     // k的类型是int,而不是int& 
  
  Derived object; 
  auto&    same_object = (Base&)object;       
  auto  another_object = (Base&)object;  //会重新构造个Base对象   
  
     
2 decltype 有时会自动把 引用 加上 

  int x; 
  decltype((x)) 和 decltype(*&x) 的类型是int&,而不是int 
  
  在宏中使用decltype时,要特别注意别多加了括号。 
  
  下面这段代码错在哪里? 
    template<typename T, typename R> 
    auto min(T t, R r) -> decltype(t < r ? t : r) 
    { 
      return (t < r ? t : r); 
    } 
     
  decltype(t < r ? t : r)的类型是T&或R&,而不是所希望的T或R! 
  
  
  标准是这样规定的: 

The type denoted by decltype(e) is defined as follows: 
  — if e is an unparenthesized id-expression or an unparenthesized class member 
     access (5.2.5), decltype(e) is the type of the entity named by e. If there 
     is no such entity, or if e names a set of overloaded functions, the program 
     is ill-formed; 
  — otherwise, if e is an xvalue, decltype(e) is T&&, where T is the type of e; 
  — otherwise, if e is an lvalue, decltype(e) is T&, where T is the type of e; 
  — otherwise, decltype(e) is the type of e. 
  
更多内容请移步至:http://zhan.renren.com/lichedu?tagId=1968&page=2&from=pages,感谢您的支持和帮助
[解决办法]
这个是晒技术的帖子?那我只好接分了~

热点排行