You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When an function from template class SecondClass calls a template function which takes a function as it template argument from template class FirstClass, the Arduino IDE treats the <> as the operator<. Note that this is happening ONLY with template classes. If I remove the templates from the classes without removing the templates from their member functions, it compiles fine.
Apart from the Arduino-specific code, it compiles without any errors or warnings in Visual Studio 2019.
How to reproduce
// Returns the length of the given stringtemplate<typename _Ty1>
constunsignedintlen(const _Ty1* const _str) noexcept{
for(unsignedint length = 0; ; length++){
if(! _str[length]){
return length;
}
}
}
// Comparison functiontemplate<typename _Ty1>
inlineconstboolCaseSensitive(const _Ty1& _ch1, const _Ty1& _ch2) noexcept{
return _ch1 == _ch2;
}
// Template type of comparison functiontemplate<typename _Ty1>
using CompFunc = constbool(*)(const _Ty1&, const _Ty1&) noexcept;
template<typename _Ty1>
classFirstClassfinal{
public:// Takes a function as its template argument and 2 characters to comparetemplate<CompFunc<_Ty1> _comp>
inlineconstboolFirstAreSame(const _Ty1& _ch1, const _Ty1& _ch2) constnoexcept{
return_comp(_ch1, _ch2);
}
};
template<typename _Ty1>
classSecondClassfinal{
public:constboolSecondAreSame(const FirstClass<_Ty1>& _first_object, const _Ty1* const _str1, const _Ty1* const _str2) constnoexcept{
constunsignedint str1_len = len<_Ty1>(_str1);
constunsignedint str2_len = len<_Ty1>(_str2);
if(str1_len != str2_len){
returnfalse;
}
for(unsignedint character = 0; character < str1_len; character++){
// It calls FirstClass<_Ty1>::FirstAreSame<>if(! (_first_object.FirstAreSame<CaseSensitive>(_str1[character], _str2[character]))){
returnfalse;
}
}
returntrue;
}
};
voidsetup(){}
voidloop(){
const FirstClass<char> first_object = FirstClass<char>();
const SecondClass<char> second_object = SecondClass<char>();
constchar* const str1 = "this is a test";
constchar* const str2 = "this is a test";
if(second_object.SecondAreSame(first_object, str1, str2)){
Serial.write("Strings are same!");
}
else{
Serial.write("Strings are NOT same!");
}
exit(0);
}
Error message
C:\Users\username\Arduino Project\Arduino.ino: In instantiation of 'const bool SecondClass<_Ty1>::SecondAreSame(const FirstClass<_Ty1>&, const _Ty1*, const _Ty1*) const [with _Ty1 = char]':
C:\Users\username\Arduino Project\Arduino.ino:63:57: required from here
Arduino:47:36: error: invalid operands of types '<unresolved overloaded function type>' and '<unresolved overloaded function type>' to binary 'operator<'
if(! (_first_object.FirstAreSame<CaseSensitive>(_str1[character], _str2[character]))){
~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
exit status 1
invalid operands of types '<unresolved overloaded function type>' and '<unresolved overloaded function type>' to binary 'operator<'
What I'm using
Arduino IDE 1.8.13
Arduino Uno 3
Windows 10 64-bit
The text was updated successfully, but these errors were encountered:
This is not an Arduino-specific problem, I think you're code is incorrect. Running this against normal g++ produces the same error. Using clang provides a hint about the problem:
matthijs@grubby:~$ clang-10 -c testfoo.cpp
testfoo.cpp:19:58: error: exception specifications are not allowed in type aliases
using CompFunc = const bool(*)(const _Ty1&, const _Ty1&) noexcept;
^
testfoo.cpp:46:24: error: missing 'template' keyword prior to dependent template name 'FirstAreSame'
if(! (_first_object.FirstAreSame<CaseSensitive>(_str1[character], _str2[character]))){
^
testfoo.cpp:62:19: note: in instantiation of member function 'SecondClass<char>::SecondAreSame' requested here
if(second_object.SecondAreSame(first_object, str1, str2)){
^
(Here, testfoo.cpp. is your code minus the references to Serial and exit)
So apparently you need to explicitly state to the compiler that FirstAreSame is a template using the template keyword. I'm not sure off-hand what the exact rules are here, but I think you can figure it out from here.
When an function from template class
SecondClass
calls a template function which takes a function as it template argument from template classFirstClass
, the Arduino IDE treats the<>
as theoperator<
. Note that this is happening ONLY with template classes. If I remove the templates from the classes without removing the templates from their member functions, it compiles fine.Apart from the Arduino-specific code, it compiles without any errors or warnings in Visual Studio 2019.
How to reproduce
Error message
What I'm using
The text was updated successfully, but these errors were encountered: