0

I have always known that special member functions of C++ are:

  • Default constructor
  • Copy Constructor
  • Copy assignment operator
  • Destructor
  • Move constructor
  • Move assignment operator

Now I am reading Meyers Effective C++ book and have realized that there is also pair of address-of operators.

I can redefine it this way:

class A
{
public:
  A* operator&()
  {
    std::cout << "Address of operator" << std::endl;
  }
};

int main()
{
  A a;
  B* b = &a; // Will call address-of operator.
}

Why then in C++ standard section 12 (Special member functions) there is no word about this operator.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Ashot Khachatryan
  • 2,156
  • 2
  • 14
  • 30
  • 4
    May be because it's not considered as a _special member function_, but an `operator` and is covered in that section? – πάντα ῥεῖ Mar 01 '15 at 14:09
  • possible duplicate of [Why would anyone want to overload the & (address-of) operator?](http://stackoverflow.com/questions/6499502/why-would-anyone-want-to-overload-the-address-of-operator) – w.b Mar 01 '15 at 14:10
  • πάντα ῥεῖ Copy assignment operator also is an operator but it is well defined – Ashot Khachatryan Mar 01 '15 at 14:11
  • @w.b No, the OP asks where it's covered in the standard. _@Ashot_ You have noticed that only these two (assignment/move) operators are mentioned as _special member functions_? – πάντα ῥεῖ Mar 01 '15 at 14:12
  • @AshotKhachatryan, what exactly is your question? – Vadim Prozorov Mar 01 '15 at 14:13
  • @VadimProzorov My question is - C++ Standard Says: The default constructor, copy constructor and copy assignment operator, move constructor and move assignment operator, and destructor are special member functions. Who is address-of operator then? – Ashot Khachatryan Mar 01 '15 at 14:14
  • 1
    *"This is also common question in C++ interviews."* I don't think so... – Christian Hackl Mar 01 '15 at 14:15
  • The address-of operator isn't even a member function unless you provide an overload, much less a special one. – molbdnilo Mar 01 '15 at 14:46
  • @Christian Hackl Maybe the question in interviews sounds like what are special member functions. – Vlad from Moscow Mar 01 '15 at 14:46
  • @VladfromMoscow: Yes, this I could imagine. But certainly not a question about "C++ standard section 12". – Christian Hackl Mar 01 '15 at 14:47
  • @Christian Hackl Why not? For example What is described in section 12 of the C++ Standard? Do you read ever the C++ Standard?:) – Vlad from Moscow Mar 01 '15 at 14:51
  • 1
    @VladfromMoscow: I would not want to work for a company which expects me to know *section numbers* of the C++ standard by heart. If they bring a copy of the standard to the interview and want to discuss some part of it, fair enough. – Christian Hackl Mar 01 '15 at 15:13
  • @Christian Hackl It is a question about whether you read ever C++ Standard.:) Most programmers not only read any Draft of the Standard but even never see it. :) – Vlad from Moscow Mar 01 '15 at 15:33
  • The name is Meyers as in Scott Meyers. – user515430 Mar 01 '15 at 17:31
  • @ChristianHackl Actually they don't directly ask about C++ standard. The question sounds like "which member functions will be defined implicitly in empty class"? And when I mentioned special member functions I know, they said there is one more. It seems like they read the same edition of Meyers book without understanding :))) – Ashot Khachatryan Mar 01 '15 at 17:46
  • @AshotKhachatryan: I just wrote an answer citing Meyers' very own errata for this. – Christian Hackl Mar 01 '15 at 17:49

3 Answers3

3

This should probably be an answer, not a comment, so be it:

It's a mistake in your edition of Effective C++. The copy I have says:

If you don't declare them yourself, your thoughtful compilers will declare their own versions of a copy constructor, an assignment operator, and a destructor.

As you can see, there is no more mention of any address-of operator. The errata for the second edition explicitly mention this change:

A class declaring no operator& function(s) does NOT have them implicitly declared. Rather, compilers use the built-in address-of operator whenever "&" is applied to an object of that type. This behavior, in turn, is technically not an application of a global operator& function. Rather, it is a use of a built-in operator.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
2

"Why then in C++ standard section 12 (Special member functions) there is no word about this operator. "

Because this operator isn't a special member function. It's actually covered in this section

13.5 Overloaded operators
1 A function declaration having one of the following operator-function-ids as its name declares an operator function. A function template declaration having one of the following operator-function-ids as its name declares an operator function template. A specialization of an operator function template is also an operator function. An operator function is said to implement the operator named in its
operator-function-id.

operator-function-id:
operator operator
operator: one of

new delete new[] delete[]
+ - * / % ˆ & | ∼
! = < > += -= *= /= %=
ˆ= &= |= << >> >>= <<= == !=
<= >= && || ++ -- , ->* ->
( ) [ ]

...

2 Both the unary and binary forms of + - * & can be overloaded.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

If you would read the Standard more closely you find that special member functions are those functions that the compiler can declare implicitly if you will not declare them explicitly.

From the C++ Standard:

12 Special member functions [special] 1 The default constructor (12.1), copy constructor and copy assignment operator (12.8), move constructor and move assignment operator (12.8), and destructor (12.4) are special member functions. [ Note: The implementation will implicitly declare these member functions for some class types when the program does not explicitly declare them. The implementation will implicitly define them if they are odr-used (3.2). See 12.1, 12.4 and 12.8. —end note ] An implicitly-declared special member function is declared at the closing } of the class-specifier. Programs shall not define implicitly-declared special member functions.

By the way the definition of the operator you showed is wrong becuase it returns nothing.

As for other member functions including operators that for example shall be declared as class members then the implementation does not declare them implicitly. It is the programmer who decides whether to declare some operators or not. For example your class may contain a dozen of assignment operators.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I read this part carefully. So from your answer I can assume that if I did not explicitly declare address-of operator program will not declare it implicitly? In this case why Meyers wrote - If you don't declare them yourself, your thoughtful compilers will declare their own versions of a copy constructor, an assignment operator, a destructor, and a pair of address-of operators? – Ashot Khachatryan Mar 01 '15 at 14:29
  • @Ashot Khachatryan I have given a quote from the Standard about special member functions.. What is not clear in the quote? Where do you have seen that the implementation will declare implicitly the address operator? – Vlad from Moscow Mar 01 '15 at 14:31
  • Quote from the standard - "The implementation will implicitly declare these member functions for some class types when the program does not explicitly declare them." So please answer the question: will address-of operator be implicitly declared if I didn't declare it explicitly? – Ashot Khachatryan Mar 01 '15 at 14:36
  • I read it very carefully and it is clear to me like 1 + 1, could you please answer the question I asked? – Ashot Khachatryan Mar 01 '15 at 14:39
  • @Ashot Khachatryan Ashot in the quote there are listed the functions that the implementation declares implicitly if you will not declare them yourself explicitly. Where do you have seen there the address-of operator? – Vlad from Moscow Mar 01 '15 at 14:41
  • So I asked this question just because I have not seen there the address operator and my question just about that. I want to repeat it one more time Meyers says - "If you don't declare them yourself, your thoughtful compilers will declare their own versions of a copy constructor, an assignment operator, a destructor, and a pair of address-of operators." So if the compiler will implicitly declare address-of operator then it should be special member function. If not then Meyers wrong. – Ashot Khachatryan Mar 01 '15 at 14:51
  • @Ashot Khachatryan If there is written indeed such a way in the book as you have said then it is obvious that it is a typo in the book. Usually books contain many inaccuracies. – Vlad from Moscow Mar 01 '15 at 14:54
  • @AshotKhachatryan: My copy of the book says "If you don't declare them yourself, your thoughtful compilers will declare their own versions of a copy constructor, an assignment operator, and a destructor." No mention of address-of operators. Perhaps you have the 1st edition of the book, and the mistake was fixed in the 2nd edition, which I have. – Christian Hackl Mar 01 '15 at 16:03