Bug 88744 - class non-type template parameters doesn't work with default template parameters
Summary: class non-type template parameters doesn't work with default template parameters
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 9.0
: P3 normal
Target Milestone: ---
Assignee: Marek Polacek
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2019-01-07 18:14 UTC by Marek Polacek
Modified: 2019-01-08 23:55 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2019-01-08 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Marek Polacek 2019-01-07 18:14:36 UTC
This ought to work:

struct S {
  int a;
  int b;
};

template<S s = {1, 2}> struct X {};
X x; // ok, X<{1, 2}>

but we reject it with:

z.C:7:3: error: class template argument deduction failed:
    7 | X x; // ok, X<{1, 2}>
      |   ^
z.C:7:3: error: no matching function for call to ‘X()’
z.C:6:31: note: candidate: ‘template<S s> X()-> X<s>’
    6 | template<S s = {1, 2}> struct X {};
      |                               ^
z.C:6:31: note:   template argument deduction/substitution failed:
z.C:6:21: error: could not convert ‘{1, 2}’ from ‘<brace-enclosed initializer list>’ to ‘S’
    6 | template<S s = {1, 2}> struct X {};
      |                     ^
Comment 1 Marek Polacek 2019-01-08 23:10:48 UTC
But actually this works so I'm not sure if there's anything to be done except adding the test.

#define SA(X) static_assert((X),#X)

struct S {
  int a;
  int b;
  constexpr S(int, int) : a(1), b(2) { }
};

template<S s = {1, 2}>
struct X {
  static constexpr int i = s.a;
  static constexpr int j = s.b;
};
X x; // ok, X<{1, 2}>

SA (x.i == 1);
SA (x.j == 2);
Comment 2 Marek Polacek 2019-01-08 23:20:28 UTC
Err, this one:

#define SA(X) static_assert((X),#X)

struct S {
  int a;
  int b;
  constexpr S(int a_, int b_) : a{a_}, b{b_} { }
};

template<S s = {1, 2}>
struct X {
  static constexpr int i = s.a;
  static constexpr int j = s.b;
};
X x; // ok, X<{1, 2}>
X<{3, 4}> x2;

SA (x.i == 1);
SA (x.j == 2);
SA (x2.i == 3);
SA (x2.j == 4);
Comment 3 Marek Polacek 2019-01-08 23:55:22 UTC
Author: mpolacek
Date: Tue Jan  8 23:54:47 2019
New Revision: 267744

URL: https://gcc.gnu.org/viewcvs?rev=267744&root=gcc&view=rev
Log:
	PR c++/88744
	* g++.dg/cpp2a/nontype-class12.C: New test.

Added:
    trunk/gcc/testsuite/g++.dg/cpp2a/nontype-class12.C
Modified:
    trunk/gcc/testsuite/ChangeLog
Comment 4 Marek Polacek 2019-01-08 23:55:51 UTC
Added.