Tim
Tim

Reputation: 5691

Reference constructor

This is rather a quick question.

I have a function:

template<class T> T& read(T& value)
{
    // Read an value of type T and assign it to variable "value"
    return value;
}

Now I can use the function as:

char c;
read(c);

But I want to use this function as:

char c = read(char());

Is that possible, or do I have to use it as:

char c = read<char>();

? (I know in the latter I have to adjust the function a bit)

Upvotes: 1

Views: 108

Answers (5)

Tim
Tim

Reputation: 5691

All right, thanks for your comments, I noticed that not all of you understanded my question. But some did, and with their help I have now these two class functions:

template<class T> void read(T& value)
{
    event::deserialize(value, *this);
}

template<class T> T read()
{
    T temp;
    event::deserialize(temp, *this);
    return temp;
}

Note that these functions are members of a class named Stream.

Thank you for your help!

Upvotes: 0

daniel gratzer
daniel gratzer

Reputation: 53901

The first one you gave doesn't actually make sense. Think about it, you pass in an anonymous value, no variable is bound to it. How can you assign to it then? What would be changed?

Since char() is an rvalue, you can't use it as a non-const reference, that you can only do with an lvalue

In this case references all together aren't necessary. If you wanted to do what your explained in your question try:

template<typename T>
T readT(){
  T v;
  read(v);
  return v;
}

Now we can type this:

char c = readT<char>();

Which does what you want.

Upvotes: 0

BigBoss
BigBoss

Reputation: 6914

In your template you say that you want to the compiler that you have a function that get an editable reference to type T and return an editable reference to that type, so you can use it as : char c; read( c ); but you can't use it as read( char() ), since char() is not editable(l-value), since you can't use it in left side of equal operator: char() = 3 is invalid. If you don't want to change input you can have something like:

template< class T >
T read( const T& val ) {
    // do something with val and return a T
    return val;
}

then you can call it as :

read( char() );
read (char)1 );
char c = read( (char)2 );
read( c );

and if you want a default value then it is just as simple as previous example:

template< class T >
T read( const T& val = T() ) {
    // do something with val and return a T
    return val;
}

and then you are able to call char c = read<char>()

Upvotes: 0

Axel
Axel

Reputation: 14169

No, you can't, and probably don't want to either. You cannot bind a temporary to a non-const reference parameter. What's the use of returning value and assigning to the parameter anyway? This doesn't make sense.

Just use this:

template<class T> T read()
{
    // Read an value of type T and assign it to variable "value"
    return value;
}

EDIT: removed the & from the return value.

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258648

No it is not, because a temporary (your char()) cannot bind to a non-const reference. The reference can't be const due to your requirements ("Read an value of type T and assign it to variable "value"").

An alternative:

template<class T> T read()
{
    // Read an value of type T and assign it to variable "value"
    return T();
}

//...
c = read<char>();

Upvotes: 1

Related Questions