473,804 Members | 2,673 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[c++0x] name deduction in overloading with rvalue references

Take the following snip (pretend A its a std::pair if you wish ;) )

template <class T1,class T2>
struct A {
A(A const&){cout<<" A(const&) ";}
A(A&&){cout<<"A (A&&) ";}
A(T1 const&, T2 const&){cout<<" A(T1,T2) ";}

template<class U1, class U2>
A(A<U1,U2&&) {cout<<"A(A<U1, U2>&&) ";}
template<class U, class... Args>
A(U&&,Args&&... ){cout<<"A(U&&, Args&&...) ";}
};

int main(){
A<int,inta(1,1) ;
A<int,intb(a);
}

The output (gcc 4.3.0 20070824 with -std=c++0x) is:
A(U&&,Args&&... ) A(U&&,Args&&... )

If I was to guess I would have thought:
A(T1,T2) A(const&)

So, the literal 1's are rvalues of type int&& I guess. So when the
params are deduced for the templated ctor with the variadic parameter,
I'm guessing U = int, Args... = int. Since 1 is an rvalue, A(int&&,
int&&) is the best match.

Ok. The next one, 'a' is an Lvalue of type A<int,int>. U then is
deduced to type A<int,int>& (N2639 14.8.2.1.3 I guess). I take it the
&& is dropped but I'm not sure how during the deduction process. I
guess it looks at parameter type U, see's that the argument is type
U&& but the REAL argument type is an lvalue so it converts the entire
argument type to an lvalue reference (unlike the first case where
int&& is the argument type, the real argument type is rvalue of type
int, so int&& is valid).

So, I go and add remove_referenc e<>'s to the parameter types (U and
Args). Now, in the first case A(T1,T2) is picked. I'd take a stab and
say that in "remove_referen ce<U>::type &&", the remove_referenc e is
applied AFTER the argument type is deduced to be int&& ? So the
argument type is... int? And the constant lvalue reference is picked
over an lvalue of type int when the actual argument type is an rvalue
int?

My head spins when I try to figure this stuff out so I wouldn't mind
if someone shed some light on the deduction process in the two cases
(that is, with and without remove_referenc e).

Much appreciated,
Chris

Aug 24 '07 #1
1 1727
In article <11************ **********@x35g 2000prf.googleg roups.com>,
Chris Fairles <ch***********@ gmail.comwrote:
Take the following snip (pretend A its a std::pair if you wish ;) )

template <class T1,class T2>
struct A {
A(A const&){cout<<" A(const&) ";}
A(A&&){cout<<"A (A&&) ";}
A(T1 const&, T2 const&){cout<<" A(T1,T2) ";}

template<class U1, class U2>
A(A<U1,U2&&) {cout<<"A(A<U1, U2>&&) ";}
template<class U, class... Args>
A(U&&,Args&&... ){cout<<"A(U&&, Args&&...) ";}
};

int main(){
A<int,inta(1,1) ;
A<int,intb(a);
}

The output (gcc 4.3.0 20070824 with -std=c++0x) is:
A(U&&,Args&&... ) A(U&&,Args&&... )

If I was to guess I would have thought:
A(T1,T2) A(const&)

So, the literal 1's are rvalues of type int&& I guess. So when the
params are deduced for the templated ctor with the variadic parameter,
I'm guessing U = int, Args... = int. Since 1 is an rvalue, A(int&&,
int&&) is the best match.

Ok. The next one, 'a' is an Lvalue of type A<int,int>. U then is
deduced to type A<int,int>& (N2639 14.8.2.1.3 I guess). I take it the
&& is dropped but I'm not sure how during the deduction process. I
guess it looks at parameter type U, see's that the argument is type
U&& but the REAL argument type is an lvalue so it converts the entire
argument type to an lvalue reference (unlike the first case where
int&& is the argument type, the real argument type is rvalue of type
int, so int&& is valid).

So, I go and add remove_referenc e<>'s to the parameter types (U and
Args). Now, in the first case A(T1,T2) is picked. I'd take a stab and
say that in "remove_referen ce<U>::type &&", the remove_referenc e is
applied AFTER the argument type is deduced to be int&& ? So the
argument type is... int? And the constant lvalue reference is picked
over an lvalue of type int when the actual argument type is an rvalue
int?

My head spins when I try to figure this stuff out so I wouldn't mind
if someone shed some light on the deduction process in the two cases
(that is, with and without remove_referenc e).
Catching up on my newsgroup reading...

A<int,inta(1,1) ;

binds to:

A(U&&,Args&&... ){cout<<"A(U&&, Args&&...) ";}

with U == int and Args == int. This is a better match than:

A(T1 const&, T2 const&){cout<<" A(T1,T2) ";}

because of the conversion to const int in each of the arguments. Note
that int does not get converted to type int&&. The '1's are just int.
It is because they are not const lvalues that is the deal killer.

Very similar story with :

A<int,intb(a);

Because 'a' is non-const it prefers the A(U&&,Args&&... ) constructor
with U == A<int, int>&, and Args = {}. When template deduction happens
against U&&, if the argument A bound is an lvalue, U is deduced as A&,
not A. This makes perfect forwarding work.

The way this should be programmed in C++0X is:

template<class U, class... Args>
requires Constructible<T 1, U&&&& Constructible<T 2, Args&&...>
A(U&&,Args&&... ){cout<<"A(U&&, Args&&...) ";}

I believe the addition of the requires clause will give you your
expected result. However I am unfortunately currently unable to compile
this to assure that I am correct.

-Howard
Sep 3 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2908
by: Bart Samwel | last post by:
Hi everybody, I would really like some help explaining this apparent discrepancy, because I really don't get it. Here is the snippet: void foo(int&); void foo(int const&); template<typename T>
14
1520
by: Steven T. Hatton | last post by:
I find writing things such as (*ptr)(arg), and (*ptr), to be at least awkward. I've often thought the following would be useful as an alternative form of ptr->operator(arg), ptr->operator: ptr->(arg) ptr-> Some people may find such an expression unintelligible. If it could be made to work, I believe it would be quickly learned, and probably used by a good number of programmers. Does anybody know of a purely technical reason that
7
1536
by: Dilip | last post by:
This is just an academic question but is there any particular reason why this does not work? template<typename T> class Foo { public: Foo(T x) : myvar_(x) { } private: T myvar_;
1
13907
by: =?Utf-8?B?c2F0aGVlc2t1bWFy?= | last post by:
In my project i have a component named PTS Engine which is developed in VC++ ..Net 2003. And it is migrated to VC++.NET 2005 version. Initially this migration process has coded and tested in VC++ .NET 2005 Beta version. In beta version everything is working fine. When i tryied to run in .NET 2005 full version i am facing the following access violation Error. "Unhandled exception at 0x4bc0145c in Engine.exe: 0xc0000005: Access Violation...
8
4580
by: minseokoh | last post by:
Hi, Could someone explain why "const" is located after the function name and what it means? inline unsigned char *access(int off) const { if (off < 0) abort(); return (&bits_); }
5
5941
by: sendos | last post by:
Consider the following sample code #include <iostream> using namespace std; class A { public: int x; A(int n = 0) : x(n) {};
8
2979
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular, operator =, operator, operator(), and operator-must be nonstatic member function; this ensures that their first operands will be lvalues". I know that these operators must be nonstatic member functions, but why this ensure their first operands will...
2
1797
by: LewGun | last post by:
at the end of last year Herb Sutter told us that "C++ 0x has been fixed", now GCC4.3 had been released, the compiler were known as "the C ++ new features' experimental unit",but it support to the new features is very limited, what do you think about the "C++0x" and the standard compilers will come in future?
0
1345
by: Frank Bergemann | last post by:
Using gcc-4.3.0 it silenty(!) invokes the Test::operator=(Test const& t) for Test a; Test b = std::move(a); Test c = std::moved(T()); - if i disable the Test::operator=(Test&& t) in source file. (But uses Test::operator=(Test&& t) if it is availabled.)
0
10569
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10315
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9140
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.