Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Under what circumstances would a type's conversion operator to itself be invoked?

Consider a type bar which has user-defined conversion operators to references of type bar:

struct bar
{
  operator bar & ();
  operator const bar & () const;
};

When would these conversions be applied? Moreover, what does it imply if these operators were deleted? Is there any interesting use of either feature?

The following program does not appear to apply either conversion:

#include <iostream>

struct bar
{
  operator bar & ()
  {
    std::cout << "operator bar &()" << std::endl;
    return *this;
  }

  operator const bar & () const
  {
    std::cout << "operator const bar &() const" << std::endl;
    return *this;
  }
};

void foo(bar x)
{
}

int main()
{
  bar x;

  bar y = x;         // copy, no conversion

  y = x;             // assignment, no conversion

  foo(x);            // copy, no conversion

  y = (bar&)x;       // no output

  y = (const bar&)x; // no output

  return 0;
}
like image 344
Jared Hoberock Avatar asked Dec 02 '11 01:12

Jared Hoberock


People also ask

What does a conversion operator do?

A conversion operator, in C#, is an operator that is used to declare a conversion on a user-defined type so that an object of that type can be converted to or from another user-defined type or basic type. The two different types of user-defined conversions include implicit and explicit conversions.

Which operator can be used to do type conversion?

operator int(); ... }; The operator overloading defines a type conversion operator that can be used to produce an int type from a Counter object. This operator will be used whenever an implicit or explict conversion of a Counter object to an int is required. Notice that constructors also play a role in type conversion.

What is a conversion operator in CPP?

Conversion Operators in C++ C++ supports object oriented design. So we can create classes of some real world objects as concrete types. Sometimes we need to convert some concrete type objects to some other type objects or some primitive datatypes. To make this conversion we can use conversion operator.

What is the use of type conversion write the standard conversion rules in C++?

A conversion produces a new value of some type from a value of a different type. Standard conversions are built into the C++ language and support its built-in types, and you can create user-defined conversions to perform conversions to, from, or between user-defined types.


3 Answers

C++11 §12.3.2

A conversion function is never used to convert a (possibly cv-qualified) object to the (possibly cv-qualified) same object type (or a reference to it), to a (possibly cv-qualified) base class of that type (or a reference to it), or to (possibly cv-qualified) void

like image 91
Cheers and hth. - Alf Avatar answered Oct 19 '22 13:10

Cheers and hth. - Alf


The feature that you are allowed to define a conversion function from a type to itself, but that the conversion function is never used, can be a useful feature in template programming, where two type parameters may or may not refer to the same type, depending on the instantiation. Some code of mine relies on this feature. It saves having to provide specializations for cases where two or more of the type parameters end up referring to the same type.

like image 30
user1958486 Avatar answered Oct 19 '22 11:10

user1958486


I can't see any reason why it would ever be called. The conversion functions are called to... convert. If you already have the right type, there is absolutely no reason to add a conversion operation before the copy.

like image 39
David Avatar answered Oct 19 '22 12:10

David