Chendy
September 18, 2018, 9:25am
1
Why doesn't this code compile? I tried pretty much the same code in Visual Studio VS2017 and it compiles fine. What's going on??
Tried with Arduino 1.8.5 Windows.
template <typename T>
struct Foo
{
struct Bar
{
T data_;
};
Bar bar_;
};
template <typename T>
void func(typename Foo<T>::Bar* bar)
{
}
void setup()
{
}
void loop() {
Foo<int> foo;
foo.bar_.data_ = 17;
func<int>(&foo.bar_);
}
I get the following errors:
circular_buffer_with_ids_test:12: error: 'T' was not declared in this scope
void func(typename Foo<T>::Bar* bar)
^
circular_buffer_with_ids_test:12: error: template argument 1 is invalid
void func(typename Foo<T>::Bar* bar)
^
exit status 1
'T' was not declared in this scope
I have no problem compiling it in 1.8.5....
Maybe post a MCVE and the actual error.
Chendy
September 18, 2018, 9:34am
3
Hi septillion,
So if you copy and past that code i posted above into a sketch, you can compile?
strange.
ps i posted the errors above
system
September 18, 2018, 9:39am
4
Typically, all templated function definitions and implementations are put in a header file which is included in the sketch.
Then, the IDE CAN compile your code.
Interesting. I just copied and pasted into 1.8.4, 1.8.5 and 1.8.6.
Errors in 1.8.4 and 1.8.5. None with 1.8.6 (unused parameter warning).
pert
September 18, 2018, 9:50am
6
Previous versions of the Arduino IDE had problems with multi-line template signatures in .ino files. The workaround was to put the entire signature on one line:
template <typename T> struct Foo
{
struct Bar
{
T data_;
};
Bar bar_;
};
template <typename T> void func(typename Foo<T>::Bar* bar)
{
}
void setup()
{
}
void loop() {
Foo<int> foo;
foo.bar_.data_ = 17;
func<int>(&foo.bar_);
}
Compiles fine in Arduino IDE 1.8.5.
Templates will always work as expected in .h files because the Arduino IDE only does sketch preprocessing on .ino files.
I have found in the past the compiler is a bit weird when the template declaration i split across two lines...
template <typename T> void func(typename Foo<T>::Bar* bar)
{
}
Is fine
template <typename T>
void func(typename Foo<T>::Bar* bar)
{
}
Is not..
[edit] pert was first
Chendy
September 18, 2018, 10:00am
8
Thanks gang
hint from PaulS to put in header file solved.
Conclusion: Arduino IDE quirk?!
This below worked, once put in a_header_file.h and placed in libraries folder:
template <typename T>
struct Foo
{
struct Bar
{
T data_;
};
Bar bar_;
};
template <typename T>
void func(typename Foo<T>::Bar* bar)
{
}
Just to confirm,
template void func(typename Foo::Bar* bar)
{
}
now compiles 1.8.4, 1.8.5, 1.8.6