I'm running across an interesting problem where I'm specializing a parent class with some math, but then getting a compiler error (GCC 11.2.0, tried C++17/20/23) when I try to specialize any function used inside the child class:
#include <iostream>
template <std::size_t N>
class Test
{
public:
template <typename T>
void Foo(void)
{
}
void Bar(void)
{
std::cout << "N is " << N << "\n";
}
};
template <std::size_t N>
class Tester: public Test<5 + (N * 2)>
{
public:
void Baz(void)
{
// Causes an error:
//
// main.cpp: In member function 'void Tester<N>::Baz()':
// main.cpp:33:30: error: expected primary-expression before '>' token
// 33 | this->Foo<uint8_t>();
// | ^
// main.cpp:33:32: error: expected primary-expression before ')' token
// 33 | this->Foo<uint8_t>();
// | ^
//this->Foo<uint8_t>();
// Works fine
this->Bar();
}
};
int main()
{
Tester<1> test;
test.Baz();
}
If I comment out the Foo<uint8_t>()
operation, it compiles and prints out 7
, as expected.
If I replace the 5 + (N * 2)
with a non-math expression (e.g. 5
) it compiles just fine with the Foo<uint8_t>()
operation uncommented, and it prints out 5
.
Is this a known limitation or bug? Am I just doing something weird that isn't allowed? Seems like it should be fine, given it works with the math if I comment that call out.