gpt4 book ai didi

C++ 检查构造函数是否包含给定类型的参数

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:34:14 27 4
gpt4 key购买 nike

使用 std::is_constructible 可以质疑某个给定类型是否存在某个构造函数:

struct A {};
struct B
{
explicit B(int, A, double) {}
};

int main()
{
std::cout<<std::is_constructible<B,int,A,double>::value<<std::endl; //prints true
}

假设一个人不知道类型B。还有一种方法可以检查 B 中是否存在包含类型 A 的构造函数,而不考虑其他参数? (或者,已经足够了,在第 n 个位置包含类型 A?)


给定一个非显式构造函数,我通过使用可以隐式转换为任何类型的类型找到了一个解决方法:

struct convert_to_anything
{
template<typename T>
operator T() const
{
return T{};
}
};

int main()
{
std::cout<<std::is_constructible<B, convert_to_anything, A, convert_to_anything>::value<<std::endl;
}

(实际上,出乎我意料的是,我根据经验发现,当 explicit 被添加到 B 的构造函数中时,它似乎也能正常工作......而我认为它会阻止转换?)

尽管如此,使用此变通方法我将不得不测试所有可能的参数数量。对于第一个位置的 A 说:

std::is_constructible<B, A>::value
|| std::is_constructible<B, A, convert_to_anything>::value
|| std::is_constructible<B, A, convert_to_anything, convert_to_anything>::value
//... and so on up to a chosen maximum size.

这似乎有点不令人满意。您有更好的解决方法吗?

最佳答案

不,基本上没有其他方法可以做到这一点。正如您所建议的,可以使用编译时元编程来手动展开排列。我相信下面的通用实现已经尽可能好了。请参阅代码底部的 has_constructor_taking 别名模板及其用法。

下面的代码使用了我描述的template_worm 技术here ,这是您的 convert_to_anything 的更充实的实现。该代码适用于最新版本的 Clang 和 GCC。

#include <utility>
#include <type_traits>
#include <tuple>

namespace detail {

//template_worm CANNOT be used in evaluated contexts
struct template_worm {

template<typename T>
operator T& () const;

template<typename T>
operator T && () const;

template_worm() = default;

template<typename... T>
template_worm(T&&...);

template_worm operator+() const;
template_worm operator-() const;
template_worm operator*() const;
template_worm operator&() const;
template_worm operator!() const;
template_worm operator~() const;
template_worm operator()(...) const;
};

#define TEMPLATE_WORM_BINARY_OPERATOR(...) \
\
template<typename T> \
constexpr inline auto \
__VA_ARGS__ (template_worm, T&&) -> template_worm { \
return template_worm{}; \
} \
\
template<typename T> \
constexpr inline auto \
__VA_ARGS__ (T&&, template_worm) -> template_worm { \
return template_worm{}; \
} \
\
constexpr inline auto \
__VA_ARGS__ (template_worm, template_worm) -> template_worm { \
return template_worm{}; \
} \
/**/

TEMPLATE_WORM_BINARY_OPERATOR(operator+)
TEMPLATE_WORM_BINARY_OPERATOR(operator-)
TEMPLATE_WORM_BINARY_OPERATOR(operator/)
TEMPLATE_WORM_BINARY_OPERATOR(operator*)
TEMPLATE_WORM_BINARY_OPERATOR(operator==)
TEMPLATE_WORM_BINARY_OPERATOR(operator!=)
TEMPLATE_WORM_BINARY_OPERATOR(operator&&)
TEMPLATE_WORM_BINARY_OPERATOR(operator||)
TEMPLATE_WORM_BINARY_OPERATOR(operator|)
TEMPLATE_WORM_BINARY_OPERATOR(operator&)
TEMPLATE_WORM_BINARY_OPERATOR(operator%)
TEMPLATE_WORM_BINARY_OPERATOR(operator,)
TEMPLATE_WORM_BINARY_OPERATOR(operator<<)
TEMPLATE_WORM_BINARY_OPERATOR(operator>>)
TEMPLATE_WORM_BINARY_OPERATOR(operator<)
TEMPLATE_WORM_BINARY_OPERATOR(operator>)

template<typename T>
struct success : std::true_type {};

template<typename T, typename... Args>
struct try_construct {
static constexpr bool value = std::is_constructible<T, Args...>::value;
};

template<typename T>
struct try_construct<T, void> {

template<typename U>
static auto test(int) ->
success<decltype(U())>;

template<typename>
static std::false_type test(...);

static constexpr const bool value = decltype(test<T>(0))::value;
};

template<typename T, typename ArgTuple, typename MappedSeq>
struct try_construct_helper;

template<typename T, typename ArgTuple, std::size_t... I>
struct try_construct_helper<T, ArgTuple, std::index_sequence<I...>> {
using type = try_construct<T, typename std::tuple_element<I, ArgTuple>::type...>;
};

struct sentinel {};

template<typename Target>
using arg_map = std::tuple<Target, template_worm const &>;

constexpr const std::size_t MappedTargetIndex = 0;
constexpr const std::size_t MappedWormIndex = 1;

template<std::size_t>
using worm_index = std::integral_constant<std::size_t, MappedWormIndex>;

template<typename SeqLeft, typename SeqRight>
struct map_indices;

template<std::size_t... Left, std::size_t... Right>
struct map_indices<std::index_sequence<Left...>, std::index_sequence<Right...>> {

using type = std::index_sequence<
worm_index<Left>::value...,
MappedTargetIndex,
worm_index<Right>::value...
>;
};

template<std::size_t... Right>
struct map_indices<sentinel, std::index_sequence<Right...>> {
using type = std::index_sequence<0, worm_index<Right>::value...>;
};

template<std::size_t... Left>
struct map_indices<std::index_sequence<Left...>, sentinel> {
using type = std::index_sequence<worm_index<Left>::value..., 0>;
};

template<>
struct map_indices<sentinel, sentinel> {
using type = std::index_sequence<0>;
};

template<std::size_t IncrementBy, typename Seq>
struct increment_seq;

template<std::size_t IncrementBy, std::size_t... I>
struct increment_seq<IncrementBy, std::index_sequence<I...>> {
using type = std::index_sequence<(I + IncrementBy)...>;
};

// Checks the U constructor by passing TargetArg in every argument slot recursively
template<typename U, typename TargetArg, std::size_t TargetIndex, std::size_t Max, typename SeqOrSentinel>
struct try_constructors;

template<typename U, typename TargetArg, std::size_t TargetIndex, std::size_t Max>
struct try_constructors<U, TargetArg, TargetIndex, Max, sentinel> {
static constexpr const bool value = false;
};

template<typename U, typename TargetArg, std::size_t TargetIndex, std::size_t Max, std::size_t... I>
struct try_constructors<U, TargetArg, TargetIndex, Max, std::index_sequence<I...>> {

using next = typename std::conditional<
sizeof...(I)+1 <= Max,
std::make_index_sequence<sizeof...(I)+1>,
sentinel
>::type;

using args = arg_map<TargetArg>;

using left_seq = typename std::conditional<
TargetIndex == 0,
sentinel,
std::make_index_sequence<TargetIndex>
>::type;

using right_seq_detail = typename increment_seq<
TargetIndex,
std::make_index_sequence<sizeof...(I)-TargetIndex>
>::type;

using right_seq = typename std::conditional<
TargetIndex == (sizeof...(I)),
sentinel,
right_seq_detail
>::type;

using mapped_seq = typename map_indices<left_seq, right_seq>::type;

static constexpr const bool value = std::disjunction<
typename try_construct_helper<U, args, mapped_seq>::type,
try_constructors<U, TargetArg, TargetIndex, Max, next>
>::value;
};

// unrolls the constructor attempts using the argument counts in the SearchSeq range
template<typename T, typename TargetArg, typename SearchSeq>
struct try_constructors_outer;

template<typename T, typename TargetArg, std::size_t... TargetIndices>
struct try_constructors_outer<T, TargetArg, std::index_sequence<TargetIndices...>> {

static constexpr const bool value = std::disjunction<
try_constructors<
T,
TargetArg,
TargetIndices,
sizeof...(TargetIndices),
std::make_index_sequence<TargetIndices>
>...
>::value;
};

template<typename T, std::size_t... TargetIndices>
struct try_constructors_outer<T, void, std::index_sequence<TargetIndices...>> {
static constexpr const bool value = try_construct<T, void>::value;
};
}

// Here you go.
template<typename TargetArg, typename T, std::size_t SearchLimit = 4>
using has_constructor_taking = std::integral_constant<bool,
detail::try_constructors_outer<
T,
TargetArg,
std::make_index_sequence<SearchLimit>
>::value
>;

struct A {};

struct B {
B(int, A, double) {}
};

struct C {
C() = delete;
C(C const &) = delete;
};

static_assert(has_constructor_taking<A, B>::value, "");
static_assert(has_constructor_taking<int, B>::value, "");
static_assert(has_constructor_taking<double, B>::value, "");
static_assert(!has_constructor_taking<C, B>::value, "");
static_assert(!has_constructor_taking<const char*, B>::value, "");

static_assert(has_constructor_taking<void, A>::value, "");
static_assert(has_constructor_taking<A const &, A>::value, "");

static_assert(!has_constructor_taking<void, C>::value, "");
static_assert(!has_constructor_taking<C const &, C>::value, "");

int main() {}

关于C++ 检查构造函数是否包含给定类型的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29735942/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com