[英]Templated is_in() function (check if array contains string) in C++
我想做以下事情:
std::string b = "b";
is_in("a", { "a", "b", "c" });
is_in("d", { "a", "b", "c" });
is_in(b, { "a", "b", "c" }); // fails
is_in(b, std::array{ "a", "b", "c" });
使用模板
template<typename Element, typename Container>
bool is_in(const Element& e, const Container& c)
{
// https://stackoverflow.com/questions/20303821/how-to-check-if-string-is-in-array-of-strings
return std::find(std::begin(c), std::end(c), e) != std::end(c);
}
template<typename Element>
bool is_in(Element e, std::initializer_list<Element> l)
{
// return std::find(std::begin(l), std::end(l), e) != std::end(l);
return is_in<Element, std::initializer_list<Element>>(e, l);
}
但我收到以下錯誤(使用 GCC 9.3.0):
no matching function for call to ‘is_in(std::string&, <brace-enclosed initializer list>)’
有沒有大腦模板的家伙有建議?
對於is_in(b, { "a", "b", "c" });
, 模板參數Element
在第一個參數b
上推導為std::string
,並在第二個參數{ "a", "b", "c" }
上推導為const char*
; 他們不匹配。
您可以為is_in
提供兩個模板參數,例如
template<typename E1, typename E2>
bool is_in(E1 e, std::initializer_list<E2> l)
{
// return std::find(std::begin(l), std::end(l), e) != std::end(l);
return is_in<E1, std::initializer_list<E2>>(e, l);
}
或者使用std::type_identity
(從 C++20 開始;並且為 pre-C++20 編寫一個很容易)從類型推導中排除第二個函數參數。
template<typename Element>
bool is_in(Element e, std::initializer_list<std::type_identity_t<Element>> l)
{
// return std::find(std::begin(l), std::end(l), e) != std::end(l);
return is_in<Element, std::initializer_list<Element>>(e, l);
}
另一種方法是在比較它們之前將不匹配的字符串類型轉換為 std::string。
#include <cassert>
#include <array>
#include <string>
// Help the compiler figure out to compare "unrelated" string types
namespace details
{
template<typename type_t>
struct compare_as
{
using type = type_t;
};
template<std::size_t N>
struct compare_as<char[N]>
{
using type = std::string;
};
template<>
struct compare_as<char*>
{
using type = std::string;
};
}
// template for "array" style parameters
template<typename type_t, typename coll_t, std::size_t N>
constexpr auto is_in(const type_t& value, const coll_t(&values)[N])
{
for (const auto& v : values)
{
typename details::compare_as<coll_t>::type lhs{ v };
typename details::compare_as<type_t>::type rhs{ value };
if (lhs == rhs) return true;
}
return false;
}
// template for containers
template<typename type_t, typename coll_t>
constexpr auto is_in(const type_t& value, const coll_t& values)
{
for (const auto& v : values)
{
typename details::compare_as<type_t>::type lhs{ v };
typename details::compare_as<type_t>::type rhs{ value };
if (lhs == rhs) return true;
}
return false;
}
int main()
{
// for non-string types compile time checking is possible
static_assert(is_in(1, { 1,2,3 }));
std::string b = "b";
assert(is_in("a", { "a", "b", "c" }));
assert(!is_in("d", { "a", "b", "c" }));
assert(is_in(b, { "a", "b", "c" }));
assert(is_in(b, std::array{ "a", "b", "c" }));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.