Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a C++ conversion operator returning reference to array?

In C++ one can add implicit-conversion operators in a class or struct. For instance, 3D vector types usually include something like:

struct Vector {
    float x, y, z;
    operator float * () { return reinterpret_cast<float *>(this); }
};

to allow accessing the vector's elements with subscripts, passing to functions that want a pointer, etc. It occurred to me to wonder: can we instead write a conversion operator that returns a reference to array of float, instead of a pointer to float?

(This is of purely academic interest. I don't know what benefits a reference-to-array would have, if any, over a simple pointer.)

As a free function we can do this like:

float (&convert(Vector & v))[3]
{
    return reinterpret_cast<float(&)[3]>(v);
}

Vector v;
convert(v);

However, I haven't been able to find the right syntax to do this as a conversion operator. I've tried things like:

operator float(&)[3] ()
operator float(&())[3]
float (&operator())[3]

and various other permutations, but I just get various syntax errors (g++ 4.8.1).

Is it possible to write a conversion operator returning a reference to array, and if so, what is the syntax to do so?

like image 664
Nathan Reed Avatar asked Dec 01 '13 04:12

Nathan Reed


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 a conversion operator?

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.

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.

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.


2 Answers

In fact you can, you almost had it with the last one:

(&operator float())[3];

As for the question of whether or not a typedef is ever necessary, I think it is from reading the comments on https://stackoverflow.com/a/6755760/2925619 (which answer is what helped me get the syntax for the above as well).

Edit:

Apparently, this syntax is incorrect and returning a reference to an array is forbidden as chris discovered for us. I guess you'll just have to settle for a typedef.

like image 87
uk4321 Avatar answered Oct 05 '22 23:10

uk4321


If you are using C++11, the problem may be resolved by introducing the ref/ptr template aliases as defined below:

template<typename T>
using ref = T&;

template<typename T>
using ptr = T*;

With this aliases the declaration of reference to array conversion operator will be in form:

operator ref<float[3]>(); 

Also it make function taking/returning pointer to function/array declaration much cleaner, e.g.:

ptr<int()> foo(ptr<int(int)> bar);

Instead of:

int (*foo(int(*bar)(int)))();
like image 43
Tomasz Kamiński Avatar answered Oct 05 '22 23:10

Tomasz Kamiński