Visual C++ 2010, rvalue reference bug? -


is bug in visual c++ 2010 or right behaviour?

template<class t> t f(t const &r) {     return r; }  template<class t> t f(t &&r) {     static_assert(false, "no way"); //< line # 10     return r; }  int main() {     int y = 4;     f(y); //< line # 17 } 

i thought, function f(t &&) should never called it's called t = int &. output:

     main.cpp(10): error c2338: no way           main.cpp(17) : see reference function template instantiation 't f(t)' being compiled                     [               t=int &           ] 

update 1 know c++x0 compiler reference? i've tried comeau online test-drive not compile r-value reference.

update 2 workaround (using sfinae):

#include <boost/utility/enable_if.hpp> #include <boost/type_traits/is_reference.hpp>  template<class t> t f(t &r) {     return r; }  template<class t> typename ::boost::disable_if< ::boost::is_reference<t>, t>::type f(t &&r) {     static_assert(false, "no way");     return r; }  int main() {     int y = 4;     f(y);     // f(5); // generates "no way" error, expected. } 

update 3 of compilers trigger on static_assert(false, "no way") if no function template instantiation. workaround (thanks @johannes schaub - litb)

template<class t> struct false_ { static bool const value = false; }; ... static_assert(false_<t>::value, "no way"); 

or

static_assert(sizeof(t) == sizeof(t), "no way"); 

as understand (and may not right; specification bit complicated), template type deduction rules conspire against you.

the compiler first attempts substitute templates (it's not choosing @ point yet—just looking options) , gets:

  • t const &r matches int lvalue t = int, creating f(int const &)
  • t &&r matches int lvalue t = int& , int & && reduces int&, creating f(int &) (there rules saying in spec).

now comes selecting correct overload , later better match, because first differs in cv-qualification , later not. that's reason why when remove const, ambiguous overload error—the overloads end being same.

ad update1: gcc supports many of c++0x features. can native windows build mingw or use cygwin.

ad update2: if need separate overloads rvalue , lvalue, seems option. templates right thing kind of reference, perhaps using std::forward ensure proper resolution of functions call depending on whether got rvalue or lvalue).


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -