Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a templated user-defined conversion operator able to determine its return type?

As stated in the title, why is this possible? Normally templated functions are unable to determine their return type if it's not among the input arguments and is not specifically stated. For example:

class Foo {
public:
    template<typename T>
    operator T() { return T(); }
};

int main() {
    Foo instance;
    int someInteger = instance;
    return 0;
}

Compiles and runs without any issues even though the return type is not explicitly stated anywhere. Is the user-defined conversion operator somehow special with regards to template rules?

I realize that it is syntactically not a return type. Nonetheless, it semantically is. After all, it is the type of the object that gets returned by the operator.

Edit: The question should probably be "Why can't templated function determine their return type?" This is invalid:

template <class T>
T sizeGetterFun()
{
    return std::numeric_limits<T>::max();
}

int main() {
    int maxInt = sizeGetterFun();
    double maxDouble = sizeGetterFun();    
    return 0;
}

This is valid and achieves the same thing as requested from the invalid code.

class Foo {
public:
    template<typename T>
    operator T() 
    { 
        return std::numeric_limits<T>::max();
    }
};

Foo sizeGetterFun()
{
    return Foo();
}

int main() {
    int maxInt = sizeGetterFun();
    double maxDouble = sizeGetterFun();    
    return 0;
}

Why can't the compiler deduce the return type automatically without the need for a dummy class that implements the conversion operator? Are there any problems one can come across when using the second(working) example?

like image 359
Silvester Avatar asked Aug 04 '14 16:08

Silvester


People also ask

What is the return type of the conversion operator?

1. What is the return type of the conversion operator? Explanation: Conversion operator doesn't have any return type not even void.

What is an user-defined type conversion?

For more information, see Standard Conversions. User-defined conversions perform conversions between user-defined types, or between user-defined types and built-in types. You can implement them as Conversion constructors or as Conversion functions.

How many types are there in user-defined conversion from class to class?

There are two types of user-defined conversions: Conversion constructors and conversion functions.

How do you define implicit conversion in C++?

An implicit conversion sequence is the sequence of conversions required to convert an argument in a function call to the type of the corresponding parameter in a function declaration. The compiler tries to determine an implicit conversion sequence for each argument.


1 Answers

Because you declared a conversion function (template). A conversion function has no return type, and because it is a conversion function, it must return a T.


C++ standard section § 12.3.2 [conversion function] :

A member function of a class X having no parameters with a name of the form

  • conversion-function-id:
    • operator conversion-type-id
  • conversion-type-id:
    • type-specifier-seq conversion-declarator opt
  • conversion-declarator:
    • ptr-operator conversion-declarator opt

specifies a conversion from X to the type specified by the conversion-type-id. Such functions are called conversion functions. No return type can be specified.


Note:

If you try to explicit a return type, e.g. :

class Foo {
public:
    template<typename T>
    int operator T() { return T(); }
};

Then you'll get a compiler error (gcc gives "return type specified for 'operator T' ")

like image 140
quantdev Avatar answered Sep 23 '22 07:09

quantdev