在编译时像未知边界数组一样初始化 C++ 结构
Initialize a C++ struct like an array of unknown bound at compile time
我可以写这个并且它工作得很好:
struct Foo
{
int i;
std::string s;
};
const Foo foo[] = {
{ 42, "the answer to the ultimate questions" },
{ 23 /*initializing only the first member, 's' gets the default value*/ }
};
我想要做的是用一个结构来包装数组,这样我就可以向它添加方法:
template<typename V1, typename V2, size_t Count>
struct Map
{
std::array<std::pair<V1, V2>, Count> mappings;
//or
//std::pair<V1, V2> mappings[Count];
V1 operator()(const V2&) const;
V2 operator()(const V1&) const;
};
我想将它初始化为一个未知边界数组,如下所示:
constexpr Map<int, std::string_view, /*???*/> = {
{ 42, "the answer to the ultimate question" },
{ 23, "some other stuff" },
{ /*...*/ }
};
但是随后出现了一个问题,您需要指定我不想做的 Count
模板参数,我希望它像在数组情况下一样工作。
我认为返回此类对象的函数可以解决问题,如下所示:
template<typename V1, typename V2, typename... Args>
constexpr auto makeMap(Args... args)
{
return Map<V1, V2, sizeof...(Args)>{ args... };
}
然后允许像这样使用它:
using Item = std::pair<int, std::string_view>;
constexpr auto map = makeMap<int, std::string_view>(
Item{ 42, "the answer to the ultimate questions" },
Item{ 23, "some other stuff" }
);
但是如果你省略了Item
类型,那么模板实例化就不能推导参数类型,这就禁止了我原本想要的用法:
constexpr auto map = makeMap<int, std::string_view>(
{ 42, "the answer to the ultimate questions" },
{ 23, "some other stuff" }
);
目前我认为这是不可能的,但无论如何我还是想问一下,以防我遗漏了什么。
在对此进行研究时,我发现 a proposal 这正是我想要的。
无论如何,我很想知道任何想法。
建议使用to_array
:
template<typename V1, typename V2, size_t N>
constexpr auto makeMap(std::pair<V1, V2> const (&a)[N])
{
return Map<V1, V2, N>{ to_array<std::pair<V1, V2>>(a) };
}
constexpr auto map = makeMap<int, std::string_view>({
{ 42, "the answer to the ultimate question" },
{ 23, "some other stuff" },
{ /*...*/ }
});
如果您的编译器支持 library fundamentals TS v2,您可以在命名空间 std::experimental
内的 header <experimental/array>
中找到 to_array
的实现。
按照 Jarod42 的建议,以递归的方式,我提出了一个递归的 MakeMyMap
结构,其中有一个 static
func()
接收一个序列 std::pair<T1, T2>
参数 [观察:42 是 std::pair
个参数数量的默认上限]。
template <typename T1, typename T2, std::size_t Dim>
struct MyMap
{
std::array<std::pair<T1, T2>, Dim> map;
};
template <typename T, std::size_t>
using getTheType = T;
template <typename, typename, typename = std::make_index_sequence<42u>>
struct MakeMyMap;
template <typename T1, typename T2, std::size_t ... Is>
struct MakeMyMap<T1, T2, std::index_sequence<Is...>>
: public MakeMyMap<T1, T2, std::make_index_sequence<sizeof...(Is)-1u>>
{
using MakeMyMap<T1, T2, std::make_index_sequence<sizeof...(Is)-1u>>::func;
static auto func (getTheType<std::pair<T1, T2>, Is> const & ... ps)
{ return MyMap<T1, T2, sizeof...(Is)>{ { { ps... } } }; }
};
template <typename T1, typename T2>
struct MakeMyMap<T1, T2, std::index_sequence<>>
{
static auto func ()
{ return MyMap<T1, T2, 0u>{ }; }
};
所以你可以这样写
auto map = MakeMyMap<int, std::string>::func(
{ 42, "the answer to the ultimate questions" },
{ 23, "some other stuff" }
);
下面是完整编译(C++14就够了)的例子
#include <array>
#include <string>
#include <utility>
template <typename T1, typename T2, std::size_t Dim>
struct MyMap
{
std::array<std::pair<T1, T2>, Dim> map;
};
template <typename T, std::size_t>
using getTheType = T;
template <typename, typename, typename = std::make_index_sequence<42u>>
struct MakeMyMap;
template <typename T1, typename T2, std::size_t ... Is>
struct MakeMyMap<T1, T2, std::index_sequence<Is...>>
: public MakeMyMap<T1, T2, std::make_index_sequence<sizeof...(Is)-1u>>
{
using MakeMyMap<T1, T2, std::make_index_sequence<sizeof...(Is)-1u>>::func;
static auto func (getTheType<std::pair<T1, T2>, Is> const & ... ps)
{ return MyMap<T1, T2, sizeof...(Is)>{ { { ps... } } }; }
};
template <typename T1, typename T2>
struct MakeMyMap<T1, T2, std::index_sequence<>>
{
static auto func ()
{ return MyMap<T1, T2, 0u>{ }; }
};
int main ()
{
auto map = MakeMyMap<int, std::string>::func(
{ 42, "the answer to the ultimate questions" },
{ 23, "some other stuff" }
);
static_assert( std::is_same<decltype(map),
MyMap<int, std::string, 2u>>::value, "!" );
}
使用 C++17,你可以使用 std::string_view
而不是 std::string
,你可以定义 constexpr
func()
函数,所以 map
可以是constexpr
constexpr auto map = MakeMyMap<int, std::string_view>::func(
{ 42, "the answer to the ultimate questions" },
{ 23, "some other stuff" }
);
你也可以验证
static_assert( std::is_same<decltype(map),
MyMap<int, std::string_view, 2u> const>::value, "!" );
使用新的 C++17 可变参数 using
,您可以完全避免递归重写 MakeMyMap
,如下所示
template <typename, typename, typename = std::make_index_sequence<42u>>
struct MakeMyMap;
template <typename T1, typename T2, std::size_t ... Is>
struct MakeMyMap<T1, T2, std::index_sequence<Is...>>
: public MMM_helper<T1, T2, std::make_index_sequence<Is>>...
{ using MMM_helper<T1, T2, std::make_index_sequence<Is>>::func...; };
其中MMM_helper
(Make My Map helper)定义如下
template <typename, typename, typename>
struct MMM_helper;
template <typename T1, typename T2, std::size_t ... Is>
struct MMM_helper<T1, T2, std::index_sequence<Is...>>
{
static constexpr auto func (getTheType<std::pair<T1, T2>, Is> const & ... ps)
{ return MyMap<T1, T2, sizeof...(Is)>{ { { ps... } } }; }
};
以下是完整的 C++17 非递归示例
#include <array>
#include <string_view>
#include <utility>
template <typename T1, typename T2, std::size_t Dim>
struct MyMap
{
std::array<std::pair<T1, T2>, Dim> map;
};
template <typename T, std::size_t>
using getTheType = T;
template <typename, typename, typename>
struct MMM_helper;
template <typename T1, typename T2, std::size_t ... Is>
struct MMM_helper<T1, T2, std::index_sequence<Is...>>
{
static constexpr auto func (getTheType<std::pair<T1, T2>, Is> const & ... ps)
{ return MyMap<T1, T2, sizeof...(Is)>{ { { ps... } } }; }
};
template <typename, typename, typename = std::make_index_sequence<42u>>
struct MakeMyMap;
template <typename T1, typename T2, std::size_t ... Is>
struct MakeMyMap<T1, T2, std::index_sequence<Is...>>
: public MMM_helper<T1, T2, std::make_index_sequence<Is>>...
{ using MMM_helper<T1, T2, std::make_index_sequence<Is>>::func...; };
int main ()
{
constexpr auto map = MakeMyMap<int, std::string_view>::func(
{ 42, "the answer to the ultimate questions" },
{ 23, "some other stuff" }
);
static_assert( std::is_same<decltype(map),
MyMap<int, std::string_view, 2u> const>::value, "!" );
}
我可以写这个并且它工作得很好:
struct Foo
{
int i;
std::string s;
};
const Foo foo[] = {
{ 42, "the answer to the ultimate questions" },
{ 23 /*initializing only the first member, 's' gets the default value*/ }
};
我想要做的是用一个结构来包装数组,这样我就可以向它添加方法:
template<typename V1, typename V2, size_t Count>
struct Map
{
std::array<std::pair<V1, V2>, Count> mappings;
//or
//std::pair<V1, V2> mappings[Count];
V1 operator()(const V2&) const;
V2 operator()(const V1&) const;
};
我想将它初始化为一个未知边界数组,如下所示:
constexpr Map<int, std::string_view, /*???*/> = {
{ 42, "the answer to the ultimate question" },
{ 23, "some other stuff" },
{ /*...*/ }
};
但是随后出现了一个问题,您需要指定我不想做的 Count
模板参数,我希望它像在数组情况下一样工作。
我认为返回此类对象的函数可以解决问题,如下所示:
template<typename V1, typename V2, typename... Args>
constexpr auto makeMap(Args... args)
{
return Map<V1, V2, sizeof...(Args)>{ args... };
}
然后允许像这样使用它:
using Item = std::pair<int, std::string_view>;
constexpr auto map = makeMap<int, std::string_view>(
Item{ 42, "the answer to the ultimate questions" },
Item{ 23, "some other stuff" }
);
但是如果你省略了Item
类型,那么模板实例化就不能推导参数类型,这就禁止了我原本想要的用法:
constexpr auto map = makeMap<int, std::string_view>(
{ 42, "the answer to the ultimate questions" },
{ 23, "some other stuff" }
);
目前我认为这是不可能的,但无论如何我还是想问一下,以防我遗漏了什么。
在对此进行研究时,我发现 a proposal 这正是我想要的。
无论如何,我很想知道任何想法。
建议使用to_array
:
template<typename V1, typename V2, size_t N>
constexpr auto makeMap(std::pair<V1, V2> const (&a)[N])
{
return Map<V1, V2, N>{ to_array<std::pair<V1, V2>>(a) };
}
constexpr auto map = makeMap<int, std::string_view>({
{ 42, "the answer to the ultimate question" },
{ 23, "some other stuff" },
{ /*...*/ }
});
如果您的编译器支持 library fundamentals TS v2,您可以在命名空间 std::experimental
内的 header <experimental/array>
中找到 to_array
的实现。
按照 Jarod42 的建议,以递归的方式,我提出了一个递归的 MakeMyMap
结构,其中有一个 static
func()
接收一个序列 std::pair<T1, T2>
参数 [观察:42 是 std::pair
个参数数量的默认上限]。
template <typename T1, typename T2, std::size_t Dim>
struct MyMap
{
std::array<std::pair<T1, T2>, Dim> map;
};
template <typename T, std::size_t>
using getTheType = T;
template <typename, typename, typename = std::make_index_sequence<42u>>
struct MakeMyMap;
template <typename T1, typename T2, std::size_t ... Is>
struct MakeMyMap<T1, T2, std::index_sequence<Is...>>
: public MakeMyMap<T1, T2, std::make_index_sequence<sizeof...(Is)-1u>>
{
using MakeMyMap<T1, T2, std::make_index_sequence<sizeof...(Is)-1u>>::func;
static auto func (getTheType<std::pair<T1, T2>, Is> const & ... ps)
{ return MyMap<T1, T2, sizeof...(Is)>{ { { ps... } } }; }
};
template <typename T1, typename T2>
struct MakeMyMap<T1, T2, std::index_sequence<>>
{
static auto func ()
{ return MyMap<T1, T2, 0u>{ }; }
};
所以你可以这样写
auto map = MakeMyMap<int, std::string>::func(
{ 42, "the answer to the ultimate questions" },
{ 23, "some other stuff" }
);
下面是完整编译(C++14就够了)的例子
#include <array>
#include <string>
#include <utility>
template <typename T1, typename T2, std::size_t Dim>
struct MyMap
{
std::array<std::pair<T1, T2>, Dim> map;
};
template <typename T, std::size_t>
using getTheType = T;
template <typename, typename, typename = std::make_index_sequence<42u>>
struct MakeMyMap;
template <typename T1, typename T2, std::size_t ... Is>
struct MakeMyMap<T1, T2, std::index_sequence<Is...>>
: public MakeMyMap<T1, T2, std::make_index_sequence<sizeof...(Is)-1u>>
{
using MakeMyMap<T1, T2, std::make_index_sequence<sizeof...(Is)-1u>>::func;
static auto func (getTheType<std::pair<T1, T2>, Is> const & ... ps)
{ return MyMap<T1, T2, sizeof...(Is)>{ { { ps... } } }; }
};
template <typename T1, typename T2>
struct MakeMyMap<T1, T2, std::index_sequence<>>
{
static auto func ()
{ return MyMap<T1, T2, 0u>{ }; }
};
int main ()
{
auto map = MakeMyMap<int, std::string>::func(
{ 42, "the answer to the ultimate questions" },
{ 23, "some other stuff" }
);
static_assert( std::is_same<decltype(map),
MyMap<int, std::string, 2u>>::value, "!" );
}
使用 C++17,你可以使用 std::string_view
而不是 std::string
,你可以定义 constexpr
func()
函数,所以 map
可以是constexpr
constexpr auto map = MakeMyMap<int, std::string_view>::func(
{ 42, "the answer to the ultimate questions" },
{ 23, "some other stuff" }
);
你也可以验证
static_assert( std::is_same<decltype(map),
MyMap<int, std::string_view, 2u> const>::value, "!" );
使用新的 C++17 可变参数 using
,您可以完全避免递归重写 MakeMyMap
,如下所示
template <typename, typename, typename = std::make_index_sequence<42u>>
struct MakeMyMap;
template <typename T1, typename T2, std::size_t ... Is>
struct MakeMyMap<T1, T2, std::index_sequence<Is...>>
: public MMM_helper<T1, T2, std::make_index_sequence<Is>>...
{ using MMM_helper<T1, T2, std::make_index_sequence<Is>>::func...; };
其中MMM_helper
(Make My Map helper)定义如下
template <typename, typename, typename>
struct MMM_helper;
template <typename T1, typename T2, std::size_t ... Is>
struct MMM_helper<T1, T2, std::index_sequence<Is...>>
{
static constexpr auto func (getTheType<std::pair<T1, T2>, Is> const & ... ps)
{ return MyMap<T1, T2, sizeof...(Is)>{ { { ps... } } }; }
};
以下是完整的 C++17 非递归示例
#include <array>
#include <string_view>
#include <utility>
template <typename T1, typename T2, std::size_t Dim>
struct MyMap
{
std::array<std::pair<T1, T2>, Dim> map;
};
template <typename T, std::size_t>
using getTheType = T;
template <typename, typename, typename>
struct MMM_helper;
template <typename T1, typename T2, std::size_t ... Is>
struct MMM_helper<T1, T2, std::index_sequence<Is...>>
{
static constexpr auto func (getTheType<std::pair<T1, T2>, Is> const & ... ps)
{ return MyMap<T1, T2, sizeof...(Is)>{ { { ps... } } }; }
};
template <typename, typename, typename = std::make_index_sequence<42u>>
struct MakeMyMap;
template <typename T1, typename T2, std::size_t ... Is>
struct MakeMyMap<T1, T2, std::index_sequence<Is...>>
: public MMM_helper<T1, T2, std::make_index_sequence<Is>>...
{ using MMM_helper<T1, T2, std::make_index_sequence<Is>>::func...; };
int main ()
{
constexpr auto map = MakeMyMap<int, std::string_view>::func(
{ 42, "the answer to the ultimate questions" },
{ 23, "some other stuff" }
);
static_assert( std::is_same<decltype(map),
MyMap<int, std::string_view, 2u> const>::value, "!" );
}