1

The following code does not compile:

#include <boost/mpl/list_c.hpp>
enum class DataType : int {
  t1 = 0,
  t2 = 1,
  t3 = 2
};
int main()
{
  boost::mpl::list_c<DataType, DataType::t1, DataType::t2> lc2;
}

I have to cast my values, like this (which compiles):

#include <boost/mpl/list_c.hpp>
enum class DataType : int {
  t1 = 0,
  t2 = 1,
  t3 = 2
};
int main()
{
  boost::mpl::list_c<int, static_cast<int>(DataType::t1), static_cast<int>(DataType::t2)> lc2;
}

I can also create the list from a variadic list of types, like this (also compiles):

#include <boost/mpl/list_c.hpp>
template<int... ints>
void create_list() {
  boost::mpl::list_c<int, ints...> lc;
}
int main()
{
  create_list<1,2,3>();
}

However, how do I create the list from a variadic list of types like below (doesn't compile):

#include <boost/mpl/list_c.hpp>
enum class DataType : unsigned char {
  t1 = 0,
  t2 = 1,
  t3 = 2
};
template<DataType... datatypes>
void create_list() {
  boost::mpl::list_c<DataType, datatypes...> lc;
}
int main()
{
  create_list<DataType::t1, DataType::t2>();
}

This gives the following compiler error:

 In instantiation of 'void create_list() [with DataType ...datatypes = {(DataType)0u, (DataType)1u}]':
13:43:   required from here
9:46: error: could not convert template argument '(DataType)0u' to 'long int'
9:46: error: could not convert template argument '(DataType)1u' to 'long int'

I also cannot cast when calling the create_list function, like this:

create_list<static_cast<unsigned char>(DataType::t1), static_cast<unsigned char>(DataType::t2)>();

since, my variadic type list has been passed by my client.

Jibbity jobby
  • 1,255
  • 2
  • 12
  • 26

2 Answers2

0

Got it:

#include <boost/mpl/list_c.hpp>
enum class DataType : unsigned char {
  t1 = 0,
  t2 = 1,
  t3 = 2
};
template<DataType... datatypes>
void create_list() {
  boost::mpl::list_c<unsigned char, static_cast<unsigned char>(datatypes)...> lc;
}
int main()
{
  create_list<DataType::t1, DataType::t2>();
}
Jibbity jobby
  • 1,255
  • 2
  • 12
  • 26
0

A list_c is a convenience builder for a sequence of integral_c

ref: http://www.boost.org/doc/libs/1_64_0/libs/mpl/doc/refmanual/list-c.html

an integral_c<T, TN> is documented as being supported where T is an integral type

An enum class is not an integral type. I think that ends the discussion.

ref: http://www.boost.org/doc/libs/1_64_0/libs/mpl/doc/refmanual/integral-c.html

Richard Hodges
  • 68,278
  • 7
  • 90
  • 142