Skip to content
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

can't get call_if to work because of etl::optional #951

Open
KammutierSpule opened this issue Sep 4, 2024 · 6 comments
Open

can't get call_if to work because of etl::optional #951

KammutierSpule opened this issue Sep 4, 2024 · 6 comments
Assignees

Comments

@KammutierSpule
Copy link

KammutierSpule commented Sep 4, 2024

while using delegate call_if, I got this error:

error: cannot convert 'etl::enable_if_t<true, etl::optional<esp_modem::command_result> >' {aka 'etl::optional<esp_modem::command_result>'} to 'esp_modem::command_result' in return

should it work?
if I call the delegate directly it works.

@jwellbelove jwellbelove self-assigned this Sep 7, 2024
@jwellbelove jwellbelove added bug and removed bug labels Sep 7, 2024
@jwellbelove
Copy link
Contributor

jwellbelove commented Sep 7, 2024

What is type esp_modem::command_result?
Is it an enum?

@jwellbelove
Copy link
Contributor

What type are you using as the result of call_if?

There is an implicit conversion from optional to bool, but not to the contained type.

This code compiles for me.

enum class command_result 
{
  OK,
  FAIL,
  TIMEOUT
};

static command_result function_that_returns_command_result() 
{
  return command_result::OK;
}

etl::delegate<command_result()> d1 = etl::delegate<command_result()>::create<function_that_returns_command_result>();

etl::optional<command_result> opt = d1.call_if();

command_result result = opt.value();

@KammutierSpule
Copy link
Author

I got another one

typedef etl::delegate<bool(const uint8_t* a_data, size_t a_data_size)> MyDelegateType;
  bool WriteToChannel(const uint8_t* a_rx_data, size_t a_rx_data_size) {
      return mydelegate_.call_if(a_rx_data, a_rx_data_size);
}

error: cannot convert 'etl::enable_if_t<true, etl::optional<bool> >' {aka 'etl::optional<bool>'} to 'bool' in return

This is a ESP32 IDF project.
This sounds to be some compiler setting or limitation.

@VzdornovNA88
Copy link

I got another one

typedef etl::delegate<bool(const uint8_t* a_data, size_t a_data_size)> MyDelegateType;
  bool WriteToChannel(const uint8_t* a_rx_data, size_t a_rx_data_size) {
      return mydelegate_.call_if(a_rx_data, a_rx_data_size);
}

error: cannot convert 'etl::enable_if_t<true, etl::optional<bool> >' {aka 'etl::optional<bool>'} to 'bool' in return

This is a ESP32 IDF project.
This sounds to be some compiler setting or limitation.

this follows the behavior of the standard std::optional type, which does not allow implicit type conversions except in the logical context of the if statement, due to the goals of improving the type safety of code in which there is no place for std::optional casts to integral types. This is provided by the explicit specifier for the conversion operator.

@jwellbelove
Copy link
Contributor

jwellbelove commented Sep 20, 2024

Are you compiling for C++03 or >= C++11?
operator bool() is implicit for C++03 and explicit for C++11 and above.
This is due to the fact that conversion operators cannot be explicit for C++03.

@KammutierSpule
Copy link
Author

Are you compiling for C++03 or >= C++11?
I believe it should be >=C++11 but didn't check.. or I'm not aware

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

3 participants