Skip to content

max() macro does not play well with std::max()  #165

Closed
@jgfoster

Description

@jgfoster

If I include <Arduino.h> before <vector>, then I get an error in stl_vector.h:1645 where the code refers to std::max().

	const size_type __len = size() + std::max(size(), __n);

This appears to be due to a macro definition of max() in arduino_ci-0.3.0/cpp/arduino/AvrMath.h causing the code to be expanded to:

	const size_type __len = size() + std::((size())>(__n)?(size()):(__n));

This produces error: expected unqualified-id before ‘(’ token (which makes sense).

My research suggests that this sort of use of macros should be avoided.

Unit testing BaseClass.cpp with g++... 

Last command:  $ g++ -std=c++0x -o /home/runner/work/LiquidCrystal/LiquidCrystal/unittest_BaseClass.cpp.bin -DARDUINO=100 -g -O1 -fno-omit-frame-pointer -fno-optimize-sibling-calls -fsanitize=address -D__AVR_ATmega2560__ -I/home/runner/.rubies/ruby-2.6.6/lib/ruby/gems/2.6.0/gems/arduino_ci-0.3.0/cpp/arduino -I/home/runner/.rubies/ruby-2.6.6/lib/ruby/gems/2.6.0/gems/arduino_ci-0.3.0/cpp/unittest -I/home/runner/work/LiquidCrystal/LiquidCrystal/src /home/runner/.rubies/ruby-2.6.6/lib/ruby/gems/2.6.0/gems/arduino_ci-0.3.0/cpp/arduino/Arduino.cpp /home/runner/.rubies/ruby-2.6.6/lib/ruby/gems/2.6.0/gems/arduino_ci-0.3.0/cpp/arduino/Godmode.cpp /home/runner/.rubies/ruby-2.6.6/lib/ruby/gems/2.6.0/gems/arduino_ci-0.3.0/cpp/arduino/stdlib.cpp /home/runner/.rubies/ruby-2.6.6/lib/ruby/gems/2.6.0/gems/arduino_ci-0.3.0/cpp/unittest/ArduinoUnitTests.cpp /home/runner/work/LiquidCrystal/LiquidCrystal/src/LiquidCrystal.cpp /home/runner/work/LiquidCrystal/LiquidCrystal/src/LiquidCrystal_CI.cpp /home/runner/work/LiquidCrystal/LiquidCrystal/test/BaseClass.cpp

In file included from /home/runner/.rubies/ruby-2.6.6/lib/ruby/gems/2.6.0/gems/arduino_ci-0.3.0/cpp/arduino/WString.h:8:0,
                 from /home/runner/.rubies/ruby-2.6.6/lib/ruby/gems/2.6.0/gems/arduino_ci-0.3.0/cpp/arduino/Arduino.h:13,
                 from /home/runner/work/LiquidCrystal/LiquidCrystal/src/LiquidCrystal.h:4,
                 from /home/runner/work/LiquidCrystal/LiquidCrystal/src/LiquidCrystal_CI.h:2,
                 from /home/runner/work/LiquidCrystal/LiquidCrystal/src/LiquidCrystal_CI.cpp:1:
/usr/include/c++/7/bits/stl_vector.h: In member function ‘std::vector<_Tp, _Alloc>::size_type std::vector<_Tp, _Alloc>::_M_check_len(std::vector<_Tp, _Alloc>::size_type, const char*) const’:
/home/runner/.rubies/ruby-2.6.6/lib/ruby/gems/2.6.0/gems/arduino_ci-0.3.0/cpp/arduino/AvrMath.h:20:18: error: expected unqualified-id before ‘(’ token
 #define max(a,b) ((a)>(b)?(a):(b))
                  ^
/usr/include/c++/7/bits/stl_bvector.h: In member function ‘std::vector<bool, _Alloc>::size_type std::vector<bool, _Alloc>::_M_check_len(std::vector<bool, _Alloc>::size_type, const char*) const’:
/home/runner/.rubies/ruby-2.6.6/lib/ruby/gems/2.6.0/gems/arduino_ci-0.3.0/cpp/arduino/AvrMath.h:20:18: error: expected unqualified-id before ‘(’ token
 #define max(a,b) ((a)>(b)?(a):(b))
                  ^
...Unit testing BaseClass.cpp with g++                                         ✗

But I need to do the includes in this order. If I include them in the other order then I get an error that <vector> isn't available in the Arduino compile (I have a check for MOCK_PINS_COUNT to leave out non-test code in the Arduino compiles).

The workaround is to #undef max (see line 6).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingduplicateThis issue or pull request already exists

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions