Skip to content

cores: replace max and min macros with imports from std #1783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 17, 2018

Conversation

yoursunny
Copy link
Contributor

fixes #1734

@yoursunny
Copy link
Contributor Author

TravisCI is unhappy. I tried the two examples from AzureIoT library locally (Arduino IDE 1.8.5 on Windows), and they all build correctly. I couldn't figure out what's wrong.

@stickbreaker
Copy link
Contributor

Looks like the problem is a redefinition of round() in Arduino.h

/home/travis/build/espressif/arduino-esp32/tools/xtensa-esp32-elf/xtensa-esp32-elf/include/c++/5.2.0/bits/random.tcc: In member function 'void std::poisson_distribution<_IntType>::param_type::_M_initialize()':
/home/travis/arduino_ide/hardware/espressif/esp32/cores/esp32/Arduino.h:78:22: error: expected unqualified-id before '(' token
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))

@yoursunny
Copy link
Contributor Author

@yoursunny
Copy link
Contributor Author

I’ve completed the round changes. Hopefully this can be reviewed soon.

@me-no-dev
Copy link
Member

Hey! Thanks for going through this :) It was somewhere on the list of things to do...

@me-no-dev me-no-dev merged commit 1e4bf14 into espressif:master Sep 17, 2018
@yoursunny yoursunny deleted the maxmin branch September 17, 2018 22:20
@radiumray
Copy link
Contributor

hi, we got this issue, the test code is below:

uint8_t a = 2;
uint16_t b = 6;
uint16_t c = 0;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  c = min(a, b);
  Serial.println(c);
}

void loop() {
  // put your main code here, to run repeatedly:
}

this code will got error:
no matching function for call to 'min(uint8_t&, uint16_t&)'

we think this change cause this issue:
1e4bf14#diff-50967a1be7f16cc780e7a8501c5d6c34

@yoursunny
Copy link
Contributor Author

no matching function for call to 'min(uint8_t&, uint16_t&)'

In C++, std::min requires both argument to have the same type. Otherwise, it cannot select an overload propery. See https://en.cppreference.com/w/cpp/algorithm/min
In your case, you should write: c = std::min<uint16_t>(a, b); or c = std::min(static_cast<uint16t_t>(a), b);

projectgus added a commit to projectgus/arduino-esp32 that referenced this pull request May 3, 2019
* Other Arduino cores uses a macro to redefine libc abs() to take any
  type, meaning abs(-3.3) == 3.3 not the normal libc result of 3.

* 1e4bf14 (espressif#1783) replaced similar min, max macros with c++ stdlib. However
  this change includes <algorithm> after the line which defines the abs() macro.
  <algorithm> includes <cstdlib> which undefines abs() and re-defines it.

* This means abs() becomes the plain libc version again which only takes
  integers, so abs(-3.3) == 3. As reported here:
  espressif/esp-idf#3405

This fix tries to keep in the spirit of espressif#1783 by using libstdc++. The other
option would be to include <cstdlib> before defining the abs() macro, so it
doesn't get undef-ed again later on.
me-no-dev pushed a commit that referenced this pull request May 11, 2019
* Other Arduino cores uses a macro to redefine libc abs() to take any
  type, meaning abs(-3.3) == 3.3 not the normal libc result of 3.

* 1e4bf14 (#1783) replaced similar min, max macros with c++ stdlib. However
  this change includes <algorithm> after the line which defines the abs() macro.
  <algorithm> includes <cstdlib> which undefines abs() and re-defines it.

* This means abs() becomes the plain libc version again which only takes
  integers, so abs(-3.3) == 3. As reported here:
  espressif/esp-idf#3405

This fix tries to keep in the spirit of #1783 by using libstdc++. The other
option would be to include <cstdlib> before defining the abs() macro, so it
doesn't get undef-ed again later on.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Arduino.h: error on max macro definition
4 participants