Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Deduce function argument which is a reference

Reply
Thread Tools

Deduce function argument which is a reference

 
 
WQ
Guest
Posts: n/a
 
      03-07-2011
Orginally I post this question on gamedev.net, but no useful answers,
so hope I can get help here (and seems here should be a better place
for C++ questions).

According to C++ standard 2003,
14.8.2.1, paragraph 2, the last sentence,

"If P is a reference type, the type referred to by P is used
for type deduction."

Does it mean that, if a function template takes a T & as input,
T is always be used to deduce?

For example,

template <typename T>
void myFunction(T n) { ............ }
..................
int & a = anotherIntVariable;
myFunction(a);

T is deduced as int, not int &, am I right? Does the standard mean
like that?

And my more question, can "a" be deduced as int & instead of int?
Is it possible?
From the standard, I guess not possible.

Any historical or theoretical reason that reference type
can't be deduced? Maybe because reference can't live without
the real type it references to?



 
Reply With Quote
 
 
 
 
SG
Guest
Posts: n/a
 
      03-07-2011
On 7 Mrz., 06:29, WQ wrote:
>
> Does it mean that, if a function template takes a T & as input,
> T is always be used to deduce?
>
> For example,
>
> template <typename T>
> void myFunction(T n) { ............ }
> .................
> int & a = anotherIntVariable;
> myFunction(a);
>
> T is deduced as int, not int &, am I right? Does the standard mean
> like that?


Yes. 'a' is defined to be a reference. But if used in an expression,
it is just a subexpression of type int which refers to an oject (and
hence is a so-called "lvalue expression"). You can think of it as: The
type of an *expression* is never a reference.

> And my more question, can "a" be deduced as int & instead of int?


Expressions have a type and a so-called "value category". Suppose,

int i;
int &r = i;

Then, there is no difference between the expression i and the
expression r. Both refer to the same int object, and both are
expressions of type int.

> Is it possible?


Not using template argument deduction, no. The fact that in the above
example r is a reference but i is not is not preserved in form of a
distinguishing property of an expression (type or value category).

> Any historical or theoretical reason that reference type
> can't be deduced?


It is intentional. A reference is a special beast. A reference type is
not an object type. References were introduced to support operator
overloading.

Cheers!
SG
 
Reply With Quote
 
 
 
 
Qi
Guest
Posts: n/a
 
      03-07-2011
On 2011-3-7 15:46, SG wrote:
>
> You can think of it as: The
> type of an *expression* is never a reference.
>


That summary is brilliant.

That really enlightened me and helped me to understand
reference deeper.

Thanks so much for the explanation.


--
WQ
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
r H2 deduce deduce template argument of a template class inheritingfrom a non template base? nguillot C++ 5 03-08-2009 05:56 PM
deduce element type of c-array template argument Maik C++ 2 12-03-2008 05:53 PM
Deduce function template argument Ed C++ 1 08-14-2008 08:13 PM
template function argument deduce George2 C++ 0 03-11-2008 12:39 PM
"could not deduce template argument" error gretean@comcast.net C++ 7 11-10-2006 03:03 PM



Advertisments