This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
[c++0x] pair's variadic constructor (N2369, 20.2.3.4)
- From: CoffeeBuzz <chris dot fairles at gmail dot com>
- To: libstdc++ at gcc dot gnu dot org
- Date: Mon, 27 Aug 2007 10:40:55 -0700 (PDT)
- Subject: [c++0x] pair's variadic constructor (N2369, 20.2.3.4)
template<typename _U1, typename... _Args>
pair(_U1 && __x, _Args &&... __args)
: first(std::forward<_U1>(__x)), second(std::forward<_Args>(__args)...) {}
I can't seem to find information as to why this was added to the standard.
It seems to me that its just some syntatic sugar to initializing pairs. For
example:
pair<int, pair<double, string>> p(1, 2.3, "Hi mom.");
struct A { A(int, int) {} };
pair<int, A> p(1,2,3);
However, after implementing the full spec, I get some odd deduction behavior
in all the ctor overloads. Take the following mock-pair class:
template <typename T1, typename T2>
struct P {
P(){}
P(P && p) {}
P(T1 const& t1, T2 const& t2){}
template <typename U1, typename U2>
P(P<U1,U2> const& p) {}
template <typename U1, typename U2>
P(U1&& u1, U2&& u2) {}
template <typename U1, typename U2>
P(P<U1,U2> && p) {}
template <typename U, typename... Args>
P(U && u, Args&&... args) {cout << "why?"; }
};
When I compile the following code:
P<int,int> p1(1,2);
P<int,int> p2(p1);
It seems that the construction of p2 is not done by P(P<U1,U2> const& p) or
P(P&& p) as I might have expected. Instead, P(U && u, Args&&... args) is
chosen with U being type P<int, int>& and Args is no type (empty pack).
Now this is an issue because in the pair, this variadic ctor forwards u to
"first" and args to "second" but first is type int, and not P<int,int>& and
thus errors. So, how do you stop this behavior?
Chris
--
View this message in context: http://www.nabble.com/-c%2B%2B0x--pair%27s-variadic-constructor-%28N2369%2C-20.2.3.4%29-tf4337197.html#a12354050
Sent from the gcc - libstdc++ mailing list archive at Nabble.com.