Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template argument deduction failed with default template parameter

Tags:

c++

c++17

#include <cstdint>
#include <iostream>

class MyBar {
public:
    void print() {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
    }
};

template <class Bar = MyBar>
class Foo{
public:
    Foo(const char* name, const uint32_t i) {
        Bar b;
        b.print();
    }
};

int main(int argc, char** argv) {
    auto pFoo1 = new Foo("abc", 3);
}

Compiler gave me:

template_ctor.cpp: In function ‘int main(int, char**)’:
template_ctor.cpp:21:31: error: class template argument deduction failed:
  auto pFoo1 = new Foo("abc", 3);
                               ^
template_ctor.cpp:21:31: error: no matching function for call to ‘Foo()’
template_ctor.cpp:14:2: note: candidate: template<class Bar> Foo(const char*, uint32_t)-> Foo<Bar>
  Foo(const char* name, const uint32_t i) {
  ^~~
template_ctor.cpp:14:2: note:   template argument deduction/substitution failed:
template_ctor.cpp:21:31: note:   candidate expects 2 arguments, 0 provided
  auto pFoo1 = new Foo("abc", 3);

As soon as I put <> after new Foo, it compiles.

At first, I thought <> is mandatory to hint the compiler to use the default template parameter, but then I noticed that if I drop const char* name and then I don't pass in "abc", then it also compiles.

Now, I am confused.

like image 534
HCSF Avatar asked Mar 28 '26 06:03

HCSF


1 Answers

This should compile, and is basically gcc bug 85883. This has been fixed on trunk:

struct MyBar;

template <class Bar = MyBar>
class Foo{
public:
    Foo(const char* name, int i);
};

auto pFoo1 = new Foo("abc", 3);

The example fails on gcc 8.2, but compiles on 9.

like image 193
Barry Avatar answered Mar 29 '26 18:03

Barry