2012-06-05 34 views
5

例子:boost :: call_traits - 爲什麼gcc會爲此提供false?

#include <iostream> 
#include <boost/call_traits.hpp> 
#include <type_traits> 

boost::call_traits<int>::param_type f() 
{ 
     return 1; 
} 

int main() 
{ 
     std::cout << std::boolalpha; 
     std::cout << 
     std::is_const<boost::call_traits<int>::param_type>::value 
     << std::endl; // true 
     std::cout << std::is_const<decltype(f())>::value << std::endl; // false 

} 

問:

除非,我做錯了什麼,我想我應該得到true兩個,但GCC 4.7.0輸出false後者。有什麼我失蹤?

回答

8

非類型右值永遠不會被const限定。只有類型的右值可能是const限定的。

所以,即使功能f被聲明爲返回一個const int,並且即使功能f的類型是const int(),呼叫表達f()是類型(非const)int的右值。

(在the new C++11 expression category taxonomy,呼叫表達f()int類型的prvalue該規則同樣適用: 「非類prvalues總是具有CV-不合格類型」 C++ 11§3.10/ 4指出)

相關問題