1

Found some C++ tests, one of a questions: what is diff between function signatures. Am I right with following answers?

void f(data); // 1)calls copy constructor of data to pass in function
void f(data*); // 2)data passes to function by ptr, no copy constructor called
void f(data const*); // 3)same as 2, but not allowed to change pointer, allowed to change data
void f(data* const); // 4)same as 2, but not allowed to change data, allowed to change pointer
void f(data const* const); // 5) same as 2, niether ptr and data can be changed
void f(data&); // 6) same as 2, but ref instead of ptr
void f(data const&); // 7) same as 3
void f(data&&); // 8) Refence to reference(most subtle moment to me), move constructor, depends on function original data can be erased
Beerhead
  • 35
  • 4
  • 2
    3 and 4 are backwards, same with 7 you may not change the data there either. And 8 is a [universal reference](https://stackoverflow.com/questions/33904462/whats-the-standard-official-name-for-universal-references) – Cory Kramer Nov 30 '20 at 12:56
  • 2
    And 8 is a rvalue reference, not a reference to a reference. – tkausl Nov 30 '20 at 12:57
  • 7 is not the same as 3. 3 is a mutable (possibly nullptr) pointer to a const data object or an array of const data objects, and 7 is a reference to const data. – Eljay Nov 30 '20 at 13:13
  • Thanks to everybody – Beerhead Nov 30 '20 at 13:50

2 Answers2

2

Not quite:

    1. Not necessarily copy. Other constructors can be used to initialise the parameter.
    1. and 4. are wrong way 'round.
    1. There is no such thing as "reference to a reference". That is an rvalue reference. No constructor is called when binding a reference to a value.
eerorika
  • 232,697
  • 12
  • 197
  • 326
0

As an additional statement to 8: As already mentioned here, there is no such thing like a reference to a reference (at least not as a plain fundamental C++ object/value type category). Its exact reference type depends on the usage context, but in the most cases, it's called an rvalue reference. In a non-evaluated template argument context - f being a function template and date being a typed template argument - its reference type is called forwarding reference. To stay strictly in standard terminology, it's also not a universal reference as Coy Kramer stated here since the committee didn't accept the terminology of Scott Meyers in 2015 for this for various reasons.

Secundi
  • 1,150
  • 1
  • 4
  • 12