Anycorn
Anycorn

Reputation: 51565

C++, reference "constructor"

template<typename T, typename R>
T f(R &r) { return T(r); }

int main() {
    int o;
    f<int&>(o);
}

Is this ok? Comeau didnt complain, so I assume int&(...) form is fine?

reason example:

    typename boost::mpl::if_c<
        (rank == 1),
        reference,
        tensor_ref<typename detail::array_ref<A>::type>
        >::type
    operator[](const int &i) {
        typedef typename boost::mpl::if_c<
            (rank == 1),
            reference,
            tensor_ref<typename detail::array_ref<A>::type>
                >::type T;
        return T(detail::array_ref<A>::generate(this->data()));
    }

Upvotes: 0

Views: 121

Answers (2)

Skyler Saleh
Skyler Saleh

Reputation: 3991

It is expanding to...

int& f(int & r){

   return int& (r);

}

which turns into...

int & r(o);

which is completely legal c++.

Upvotes: 0

GManNickG
GManNickG

Reputation: 504303

This is fine, it acts like a cast from int& to int&, and returns int&, referring back to o.

I'm not sure what purpose this would serve, though.

Upvotes: 2

Related Questions