arduino-cli icon indicating copy to clipboard operation
arduino-cli copied to clipboard

Due SAM: The compiler attempts to instantiate a mismatched template specialization

Open Ebola-Chan-bot opened this issue 1 year ago • 2 comments

Describe the problem

C:\Users\vhtmf\Documents\Arduino\sketch_feb10a\sketch_feb10a.ino: In instantiation of 'constexpr const bool _Allow_inheriting_unwrap<unsigned int*, void>::value':
C:\Users\vhtmf\Documents\Arduino\sketch_feb10a\sketch_feb10a.ino:22:70:   required from here
C:\Users\vhtmf\Documents\Arduino\sketch_feb10a\sketch_feb10a.ino:19:25: error: 'unsigned int*' is not a class, struct, or union type
   static constexpr bool value = is_same<_Iter, typename _Iter::_Prevent_inheriting_unwrap>::value;
                         ^

exit status 1

Compilation error: 'unsigned int*' is not a class, struct, or union type

To reproduce

template<class, class>
struct is_same {
  static constexpr bool value = false;  // determine whether arguments are the same type
};
template<class _Ty>
struct is_same<_Ty, _Ty> {
  static constexpr bool value = true;
};

template<class... _Types>
using void_t = void;

template<class _Iter, class = void>
struct _Allow_inheriting_unwrap {
  static constexpr bool value = true;
};
template<class _Iter>
struct _Allow_inheriting_unwrap<_Iter, void_t<typename _Iter::_Prevent_inheriting_unwrap>> {
  static constexpr bool value = is_same<_Iter, typename _Iter::_Prevent_inheriting_unwrap>::value;
};
void setup() {
  constexpr auto BB = _Allow_inheriting_unwrap<unsigned int*, void>::value;
}

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

Expected behavior

This code can be compiled in both MSVC and Godbolt. Even in the Arduino IDE, the value of constexpr auto BB is correctly displayed as bool true. But once it enters the compile phase, the compiler mistakenly tries to match the specialized _Allow_inheriting_unwrap and then fails because unsigned int * has no members to reference.

Arduino CLI version

0.35.2

Operating system

Windows

Operating system version

Windows 11

Additional context

This issue only appears on the Arduino Due SAM platform. The AVR platform is normal.

Issue checklist

  • [X] I searched for previous reports in the issue tracker
  • [X] I verified the problem still occurs when using the nightly build
  • [X] My report contains all necessary details

Ebola-Chan-bot avatar Feb 12 '24 13:02 Ebola-Chan-bot

This may indicate a problem with the compiler's handling of SFINAE.

Ebola-Chan-bot avatar Feb 13 '24 00:02 Ebola-Chan-bot

At present, I need to do the following workaround for void_t:

template<class... _Types>
struct void_s {
  using type = void;
};
template<class T, class... _Types>
struct void_s<T, _Types...> {
  using type = T;
};
template<typename... _Types>
using void_t = typename void_s<_Types...>::type;

If anyone else is having a similar problem, try my workaround idea.

Ebola-Chan-bot avatar Feb 13 '24 00:02 Ebola-Chan-bot