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

Macro clash with template argument I when including <complex.h> #67

Open
radareclipse opened this issue Jul 16, 2024 · 0 comments
Open
Assignees
Milestone

Comments

@radareclipse
Copy link

nameof version: 0.10.4

<complex.h> defines I as a shorthand for the imaginary constant $i$. This mangles the template argument I and causes compilation to fail.

(Context is that I'm working with math libraries that transitively include <complex.h>.)

MWE:

#include <complex.h>
#include <iostream>

#include <nameof.hpp>

class Minimal {};

int main() {
  Minimal mini{};
  std::cout << NAMEOF_SHORT_TYPE_RTTI(mini) << '\n';
  return 0;
}

Remove the <complex.h> include and compilation succeeds.

Compiler nonsense (g++ 13.3.0):

In file included from /usr/include/c++/13/complex.h:38,
                 from src/minimal.cpp:1:
nameof.hpp:276:30: error: expected ‘>’ before ‘(’ token
  276 |   template <std::uint16_t... I>
      |                              ^
In file included from src/minimal.cpp:4:
nameof.hpp:277:76: error: expansion pattern ‘(__complex__ float){0.0f, 1.0e+0f}’ contains no parameter packs
  277 |   constexpr cstring(string_view str, std::integer_sequence<std::uint16_t, I...>) noexcept : chars_{str[I]..., '\0'} {}
      |                                                                            ^~~
nameof.hpp:277:79: error: template argument 2 is invalid
  277 |   constexpr cstring(string_view str, std::integer_sequence<std::uint16_t, I...>) noexcept : chars_{str[I]..., '\0'} {}
      |                                                                               ^
nameof.hpp: In constructor ‘constexpr nameof::cstring<N>::cstring(std::string_view, int)’:
nameof.hpp:277:103: error: no match for ‘operator[]’ (operand types are ‘std::string_view’ {aka ‘std::basic_string_view<char>’} and ‘__complex__ float’)
  277 |   constexpr cstring(string_view str, std::integer_sequence<std::uint16_t, I...>) noexcept : chars_{str[I]..., '\0'} {}
      |                                                                                                       ^
In file included from /usr/include/c++/13/bits/basic_string.h:47,
                 from /usr/include/c++/13/string:54,
                 from /usr/include/c++/13/bits/locale_classes.h:40,
                 from /usr/include/c++/13/bits/ios_base.h:41,
                 from /usr/include/c++/13/ios:44,
                 from /usr/include/c++/13/istream:40,
                 from /usr/include/c++/13/sstream:40,
                 from /usr/include/c++/13/complex:45,
                 from /usr/include/c++/13/ccomplex:39,
                 from /usr/include/c++/13/complex.h:32:
/usr/include/c++/13/string_view:254:7: note: candidate: ‘constexpr const std::basic_string_view<_CharT, _Traits>::value_type& std::basic_string_view<_CharT, _Traits>::operator[](size_type) const [with _CharT = char; _Traits = std::char_traits<char>; const_reference = const char&; size_type = long unsigned int]’
  254 |       operator[](size_type __pos) const noexcept
      |       ^~~~~~~~
/usr/include/c++/13/string_view:254:28: note:   no known conversion for argument 1 from ‘__complex__ float’ to ‘std::basic_string_view<char>::size_type’ {aka ‘long unsigned int’}
  254 |       operator[](size_type __pos) const noexcept
      |                  ~~~~~~~~~~^~~~~
nameof.hpp: At global scope:
nameof.hpp:528:20: error: expected nested-name-specifier before ‘(’ token
  528 | template <typename I>
      |                    ^
nameof.hpp:528:20: error: expected ‘)’ before ‘__extension__’
  528 | template <typename I>
      |                    ^
nameof.hpp:528:20: error: expected ‘>’ before ‘__extension__’
nameof.hpp:528:20: error: expected unqualified-id before ‘)’ token
  528 | template <typename I>
      |                    ^
nameof.hpp:665:76: error: expected ‘)’ before ‘__extension__’
  665 | template <typename E, bool IsFlags, std::size_t Size, int Min, std::size_t I>
      |                                                                            ^
nameof.hpp:665:76: error: expected ‘>’ before ‘__extension__’
nameof.hpp:665:76: error: expected unqualified-id before ‘)’ token
  665 | template <typename E, bool IsFlags, std::size_t Size, int Min, std::size_t I>
      |                                                                            ^
nameof.hpp:744:52: error: expected ‘>’ before ‘(’ token
  744 | template <typename E, bool IsFlags, std::size_t... I>
      |                                                    ^
nameof.hpp:745:43: error: expansion pattern ‘(__complex__ float){0.0f, 1.0e+0f}’ contains no parameter packs
  745 | constexpr auto names(std::index_sequence<I...>) noexcept {
      |                                           ^~~
nameof.hpp:745:46: error: template argument 1 is invalid
  745 | constexpr auto names(std::index_sequence<I...>) noexcept {
      |                                              ^
nameof.hpp: In function ‘constexpr auto nameof::detail::names(int)’:
nameof.hpp:746:60: error: template argument 2 is invalid
  746 |   constexpr auto names = std::array<string_view, sizeof...(I)>{{enum_name_v<E, values_v<E, IsFlags>[I]>...}};
      |
@Neargye Neargye added this to the v0.10.5 milestone Sep 19, 2024
@Neargye Neargye self-assigned this Sep 19, 2024
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

No branches or pull requests

2 participants