Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

g++ compiles, msvc crashes, is the code correct?

22 views
Skip to first unread message

Alf P. Steinbach

unread,
Jun 6, 2020, 10:06:10 AM6/6/20
to
MSVC bug report: <url:
https://developercommunity.visualstudio.com/content/problem/1067774/ice-internal-compiler-error-on-constexpr-range-bas.html>

Since the msvc compiler crashes I have only g++'s word that the
following code is valid.

Is it?


---------------------------------------------------------------------------
#include <limits.h> // CHAR_BIT
#include <stdint.h> // int...

#include <bitset>
#include <initializer_list>
#include <iostream>
#include <string>
#include <type_traits>

template< class Type >
constexpr int bits_per_ = sizeof( Type )*CHAR_BIT;

namespace impl {
template< int n_bits > struct Int_t_;

template<> struct Int_t_<8>{ using T = int8_t; };
template<> struct Int_t_<16>{ using T = int16_t; };
template<> struct Int_t_<32>{ using T = int32_t; };
template<> struct Int_t_<64>{ using T = int64_t; };
// TODO: conditional definition for 128-bits.
} // namespace impl

template< int n_bits > using Int_ = typename impl::Int_t_<n_bits>::T;
template< int n_bits > using Unsigned_int_ =
std::make_unsigned_t<Int_<n_bits>>;

template<
class Enum,
int n_bits = bits_per_<void*>,
class = std::enable_if_t<std::is_enum_v<Enum>>
>
class Bitset
{
using Integer = std::underlying_type_t<Enum>;
using Bits = Unsigned_int_<n_bits>;

Bits m_bits;

public:
constexpr Bitset( const std::initializer_list<Enum>& values ):
m_bits()
{
for( const Enum v: values ) {
const auto i = static_cast<Integer>( v );
if( i < 0 or i > n_bits ) {
throw "Ugga bugga";
}
m_bits |= (Bits( 1 ) << i);
}
}
};

enum Values {one, two, three, five, seven};

auto main()
-> int
{
constexpr auto x = Bitset({ one, two, five });
// constexpr auto y = Bitset({ 1, 2, 5 });
(void) x;
}
#include <cppx-core-language/all.hpp>

#include <limits.h> // CHAR_BIT
#include <stdint.h> // int...

#include <bitset>
#include <initializer_list>
#include <iostream>
#include <string>
#include <type_traits>

template< class Type >
constexpr int bits_per_ = sizeof( Type )*CHAR_BIT;

namespace impl {
template< int n_bits > struct Int_t_;

template<> struct Int_t_<8>{ using T = int8_t; };
template<> struct Int_t_<16>{ using T = int16_t; };
template<> struct Int_t_<32>{ using T = int32_t; };
template<> struct Int_t_<64>{ using T = int64_t; };
// TODO: conditional definition for 128-bits.
} // namespace impl

template< int n_bits > using Int_ = typename impl::Int_t_<n_bits>::T;
template< int n_bits > using Unsigned_int_ =
std::make_unsigned_t<Int_<n_bits>>;

template<
class Enum,
int n_bits = bits_per_<void*>,
class = std::enable_if_t<std::is_enum_v<Enum>>
>
class Bitset
{
using Integer = std::underlying_type_t<Enum>;
using Bits = Unsigned_int_<n_bits>;

Bits m_bits;

public:
constexpr Bitset( const std::initializer_list<Enum>& values ):
m_bits()
{
for( const Enum v: values ) {
const auto i = static_cast<Integer>( v );
if( i < 0 or i > n_bits ) {
throw "Ugga bugga";
}
m_bits |= (Bits( 1 ) << i);
}
}
};

enum Values {one, two, three, five, seven};

auto main()
-> int
{
constexpr auto x = Bitset({ one, two, five });
// constexpr auto y = Bitset({ 1, 2, 5 });
(void) x;
}
---------------------------------------------------------------------------


- Alf

Alf P. Steinbach

unread,
Jun 6, 2020, 10:11:22 AM6/6/20
to
On 06.06.2020 16:05, Alf P. Steinbach wrote:
> MSVC bug report: <url:
> https://developercommunity.visualstudio.com/content/problem/1067774/ice-internal-compiler-error-on-constexpr-range-bas.html>
>
>
> Since the msvc compiler crashes I have only g++'s word that the
> following code is valid.
>
> Is it?

Sorry for the doubly posted code with a spurious #include added.

That was due to imprefect editing of a draft where I intended to remove
that unnecessary #include.

Thunderbird's fault, or Mozilla, or Donald Trump.

- Alf

Ian Collins

unread,
Jun 6, 2020, 6:23:41 PM6/6/20
to
On 07/06/2020 02:11, Alf P. Steinbach wrote:
> On 06.06.2020 16:05, Alf P. Steinbach wrote:
>> MSVC bug report: <url:
>> https://developercommunity.visualstudio.com/content/problem/1067774/ice-internal-compiler-error-on-constexpr-range-bas.html>
>>
>>
>> Since the msvc compiler crashes I have only g++'s word that the
>> following code is valid.
>>
>> Is it?
>
> Sorry for the doubly posted code with a spurious #include added.

It doesn't compile with anything, whats "is_enum_v"?

--
Ian.

Manfred

unread,
Jun 6, 2020, 6:59:48 PM6/6/20
to
https://en.cppreference.com/w/cpp/types/is_enum

By the way, I compiled it with gcc 9.3 and it works fine - also, to the
OP, FWIW the code is valid for me.

Ian Collins

unread,
Jun 6, 2020, 8:01:19 PM6/6/20
to
Ah, I forgot -std=c++17.

--
Ian.

0 new messages