ETLCPP / etl

Embedded Template Library
https://www.etlcpp.com
MIT License
2.25k stars 392 forks source link

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

Closed KammutierSpule closed 2 weeks ago

KammutierSpule commented 2 months ago

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 commented 2 months ago

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

jwellbelove commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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