Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The compiler doesn't complain when vector<char>&& is bound to vector<char>&

I'm using Visual Studio 2013 Express.

class B{
public:
    vector<char>& a;
    int& b;
    B(vector<char>& i,int& c) :a(i),b(c) {}
};

int main(){
    int l=3;
    vector<char> h;
    shared_ptr<B> bb (new B(std::move(h),l));
    return 0;
}

Why can the code be accepted?When I changed the argument l to std::move(l),the compiler will complain "cannot convert argument 2 from 'int' to 'int &'".

like image 745
user1265566 Avatar asked May 08 '14 14:05

user1265566


1 Answers

This is a language extension available in the Visual C++ compiler and has existed for quite some time now. The extension allows you to bind an rvalue (tempoarary) to a non-const reference and extend the lifetime of value as if you were binding to a const reference.. If you enable warning level 4 or explicitly enable warning C4239 the compiler will alert you any time the extension is used.

The documentation for C4239 includes an example that is similar to what's in your question.

like image 191
Captain Obvlious Avatar answered Nov 08 '22 09:11

Captain Obvlious