I’ve come across a strange syntax error when I made a call to a templated function in a struct that use a concept.
main.h
struct A {
template<typename T>
T get();
};
struct B : A {
template<typename T>
T get();
};
template<> int A::get() {
return 1;
}
template<> float A::get() {
return 1.0;
}
template<> int B::get() {
return 1;
}
template<> float B::get() {
return 1.0;
}
main.cpp
#include "main.h"
#include <iostream>
template <typename T>
concept DerivedFromA = std::is_base_of<A, T>::value;
struct MyStruct1 {
B _b;
MyStruct1(B b) { _b = b; }
int test() {
// Everything fine here !
return _b.get<int>();
}
};
// Note the use of concept
template<DerivedFromA D>
struct MyStruct2 {
D _d;
MyStruct2(D d) { _d = d; }
int test() {
// Here Syntax error !
return _d.get<int>();
}
};
int main() {
B b;
// // Here OK !
// std::cout << b.get<int>() << std::endl;
MyStruct1 myStruct1(b);
auto result1 = myStruct1.test();
MyStruct2 myStruct2(b);
auto result2 = myStruct2.test();
return 0;
}
This code produce the following syntax errors:
error: expected primary-expression before ‘int’ return _d.get<int>();
error: expected ‘;’ before ‘int’
Only, and only on the struct that is templated by a concept. Is there any reason for that ?