3

I'm looking up c++ library, and see the istream class, I am confused with a contractor with an address symbol. what is the meaning of a constructor with an address symbol?

one of the istream constructors is.

protected: iostream& (iostream&& x);

I found it in website cplusplus.com,

c++11contructors

link: iostream



I defined a customer class with a similar constructor that has a & symbol:

//Test.cpp
#include <iostream>/*cout,cin*/
#include <typeinfo>/*typeid(),name()*/
using namespace std;
struct MyTest{
                MyTest&(double b){}
};
int main(int argc,char* argv[]){
        MyTest mt2(2.1);
        cout << typeid(mt2).name() << endl;
return 0;
}

I use the below command to compile it:

g++ Test.cpp -o Test -std=c++11

however, I get some compile error messages:

Test.cpp:7:11: error: expected unqualified-id before ‘float’
   MyTest&(float b){}
           ^
Test.cpp:7:11: error: expected ‘)’ before ‘float’
Test.cpp:7:10: error: expected ‘;’ at end of member declaration
   MyTest&(float b){}
          ^
Test.cpp:7:17: error: expected ‘;’ at end of member declaration
   MyTest&(float b){}
                 ^
Test.cpp:7:18: error: expected unqualified-id before ‘)’ token
   MyTest&(float b){}
                  ^
Test.cpp: In function ‘int main(int, char**)’:
Test.cpp:12:16: error: no matching function for call to ‘MyTest::MyTest(double)’
  MyTest mt2(2.1);


I got confused, c++ library istream class is fine. why did my custom class constructor fail? what am I missing?

Guokas
  • 750
  • 7
  • 23
  • 2
    Where did you find `iostream& (iostream&& x);`? Could you link to the exact place in the [repo](https://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/files.html)? – Tobi Apr 15 '19 at 10:39
  • http://www.cplusplus.com/reference/istream/iostream/iostream/ – Guokas Apr 15 '19 at 10:40
  • 2
    Despite the name and its google ranking, cplusplus.com is not an authoritative, or even good, source. It has many errors. – molbdnilo Apr 15 '19 at 10:58
  • C++ has a context-dependent grammar; what you're asking about is not the **address-of operator**, but rather the **reference type-modifier**. – Toby Speight Apr 15 '19 at 11:19
  • 5
    I don't know why the down votes. This is a perfectly reasonable question about something that has been found in documentation; and OP has tried to reproduce the syntax to understand what it will do but failed. It is the failure of the community to fix google rankings/cplusplus.com; and not that of the OP who read it; and asking to understand the error IS A GOOD THING. – UKMonkey Apr 15 '19 at 12:19

2 Answers2

2

The information on cplusplus.com is... sometimes not dependable. (See What's wrong with cplusplus.com? for a discussion of this.) On CPPReference, you can see that the move constructor is, you know, just a regular move constructor.

Sneftel
  • 40,271
  • 12
  • 71
  • 104
0

This is a bug in http://www.cplusplus.com/reference/istream/iostream/iostream/.

If you look at https://en.cppreference.com/w/cpp/io/basic_iostream/basic_iostream, you will find

protected: basic_iostream( basic_iostream&& other );

Tobi
  • 2,591
  • 15
  • 34
  • given that the cplusplus.com site states: `copy (2) iostream& (const iostream&) = delete; move (3) protected: iostream& (iostream&& x);` I think you need to clarify the part that you think is wrong – UKMonkey Apr 15 '19 at 12:11