mariusbancila / stduuid

A C++17 cross-platform implementation for UUIDs
MIT License
742 stars 112 forks source link

Initialization of empty_guid and guid_encoder #86

Open SBucur opened 4 months ago

SBucur commented 4 months ago

Writing this to mainly check if this is worth creating a merge request.

I ran into a compilation error when one of my projects was compiling a CUDA C file that inevitably #includes uuid.h. The nvcc compiler returns an error when trying to deduce the data in empty_guid and guid_encoder for CharT types:

/usr/local/include/uuid.h(298): error: a value of type "const char [37]" cannot be used to initialize an entity of type "const CharT [37]"

/usr/local/include/uuid.h(304): error: a value of type "const char [17]" cannot be used to initialize an entity of type "const CharT [17]"

What worked for me was changing the initialization of the value. At compile time, writing the rhs as a const char[37] with double quotes only deduces to a const char array. You can implicitly call std::initializer_list so the compiler deduces all types of CharT:

template <typename CharT>
inline constexpr CharT empty_guid[37] =
   {'0','0','0','0','0','0','0','0',
   '-','0','0','0','0',
   '-','0','0','0','0',
   '-','0','0','0','0',
   '-','0','0','0','0','0','0','0','0','0','0','0','0'};

template <typename CharT>
inline constexpr CharT guid_encoder[17] = 
    {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};

Regardless of how the project was configured, template initialization should be written to account for the generic type.