|
Boost : |
Subject: Re: [boost] [gsoc17] Project1: Static map: Can I implement the competency test in g++-7?
From: Vic Luo (vicluo96_at_[hidden])
Date: 2017-03-10 15:58:30
Hi,
During the implementation of my static_map, I recently encountered a
problem: how to deal with string types?
For example:
constexpr auto cmap_str = make_static_map({
{"abc", 1},
{"bcd", 2}
});
static_assert(cmap_str["abc"] == 1, "test");
In my implementation, KeyType requires
1. constexpr operator==, which is not provided by const char *
2. constexpr hash(const KeyType&). Since there is no such thing in
standard library, I have to implement some in this project.
Currently I would like to use string_view, since it looks like a general
solution for compile-time string with lots of constexpr functions, but I
don't know which of the following designs looks better:
Design A:
template<typename KeyType,
typename MappedType,
typename EqualOp = StaticEqualOp<KeyType>,
typename HashOp = StaticHashOp<KeyType>>
class static_map;
// StaticEqualOp: Specialize StaticEqualOp for those KeyTypes that
// can construct string_view and use operator== of string_view,
// otherwise we simply use operator== of KeyType
// StaticHashOp: size_t operator()(const string_view&, const
string_view&)
// for types that can construct string_view
Design B:
We don't specialize any StaticEqualOp for const char*. Instead,
make_static_map({{"abc", 1}}) returns static_map<string_view, int>
instead of static_map<const char *, int>.
Which one is better? Or is there any more feasible design?
Thanks,
-- --------------------------------- Vic Luo Shanghai Jiaotong University Key fingerprint 0x98809ca08bf5662a
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk