Groups | Search | Server Info | Login | Register


Groups > comp.std.c++ > #783

C++11: Template argument deduction, partial ordering, and trailing parameter packs

Message-ID <dijkrvFh6ulU1@mid.individual.net> (permalink)
Newsgroups comp.std.c++
From Christoph Schulz <develop@kristov.de>
Subject C++11: Template argument deduction, partial ordering, and trailing parameter packs
Organization unknown
Date 2016-02-20 15:19 -0600

Show all headers | View raw


Hello,

I read §14.8.2.1, §14.8.2.4, and §14.8.2.5 multiple times but did not find
a convincing argumentation why the second of the following two function
templates is more specialized for the given argument than the first one:

========= %< =========
#include <ostream>
#include <iostream>

template<int ...T>
struct X {};

// #1
template<int I, int ...Ints>
int f(X<I, Ints...> x) {
        return I + f(X<Ints...>{});
}

// #2
template<int I>
int f(X<I>) {
        return I;
}

int main()
{
        std::cout << f(X<1>{}) << std::endl; // selects #2
}
========= %< =========

The compiler I used (g++ 4.9.3) has no problems compiling this code. But
I don't understand why. For both #1 and #2, "I" can be deduced to be "1",
and in #1, "Ints..." can be deduced to be an empty list of int values,
making the parameter type be "X<1>" in both cases. (This can be "proven"
by removing #1 or #2 from the overload set by #if 0 ... #endif, and seeing
the code compile successfully in both cases.) So, according to
§13.3.3/1, partial ordering of function templates should disambiguate this
case. But which rule makes #2 more specialized than #1? In this situation,
§14.8.2.5/9 seems to apply, but the sentence

- if P does not contain a template argument corresponding to Ai then Ai
is ignored;

seems to support the interpretation that #1 can be deduced from #2.

Can anybody shed some light on it? I'm sure that I overlooked
something... Or does g++ behave simply wrong?


Best regards,

--
Christoph Schulz


[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]

Back to comp.std.c++ | Previous | NextNext in thread | Find similar


Thread

C++11: Template argument deduction, partial ordering, and trailing parameter packs Christoph Schulz <develop@kristov.de> - 2016-02-20 15:19 -0600
  Re: C++11: Template argument deduction, partial ordering, and  trailing parameter packs Christof Meerwald <NOSPAM-seeMySig+ul15+@usenet.cmeerw.org> - 2016-03-16 01:07 -0600
  Re: C++11: Template argument deduction, partial ordering, and trailing parameter packs Christoph Schulz <develop@kristov.de> - 2016-03-16 01:07 -0600

csiph-web