leyiliu <595657419 <at> qq.com> writes:
Hi,All:
When I use the multi_index::composite_key with char[],I meet a
error,when find a element。It seems to the make_tuple can't support the
char[].Because the char[] more faster the std::string。So our project
choose char[] as the column instead of std::string.So How should I do
Hi leyiliu,
I'm not totally sure I'm getting your question, so allow to me to
make a guess:
I assume you have something like
struct X
{
int x;
char str[20];
};
using multi_index_t=multi_index_container<
X,
indexed_by<
ordered_unique<
composite_key<
X,
member,
member
>
>
>
>;
...
multi_index_t m=...;
auto it=m.lower_bound(std::make_tuple(1,"hello"));
which fails with
error: no match for call to '(const std::less)
(const char* const&, const char [20])
because std::less won't accept a const char* (the type
of "hello") as an argument. There's two dixes for this:
1. Provide a comparison for str that does the work
using multi_index_t=multi_index_container<
X,
indexed_by<
ordered_unique<
composite_key<
X,
member,
member
>,
composite_key_compare<
std::less<int>,
std::less
>
>
>
>;
std::less works equally well for const char* and
char[20] (technically, the latter *decays* to const char* when passed
to std::less::operator()).
2. Provide a key extractor that returns a const char* rather than a
reference to char[20]:
inline const char* get_str(const X& x){return x.str;}
using multi_index_t=multi_index_container<
X,
indexed_by<
ordered_unique<
composite_key<
X,
member,
global_fun
>
>
>
>;
Please report back whether this is your problem and any of 1,2 solves it.
Best regards,
Joaquín M López Muñoz
Telefónica