#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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With