- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
使用 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/
谁能解释一下原因: (define a (lambda() (cons a #f))) (car (a)) ==> procedure ((car (a))) ==> (procedure . #f)
这是 PyBrain 网站的摘录。我了解大部分正在发生的事情,但是一行让我完全难住了。我以前从未在 python 代码中看到过这样的东西。这是整个循环,对于上下文: for c in [0,
我是gradle / groovy的新手。我想创建将做一些事情的自定义任务。我的第一个问题是任务完成时该如何做?我可以覆盖doFirst / doLast闭包吗?也许我可以重写某些在开始和结束时都会执
我刚刚开始评估 MS 企业库。他们使用以下指令来获取实例: var customerDb = EnterpriseLibraryContainer.Current.GetInstance("C
这是我的 if else Ansible 逻辑.. - name: Check certs exist stat: path=/etc/letsencrypt/live/{{ rootDomain
我正在使用construct 2.8 对一些失传已久的 Pascal 程序创建的一些文件的 header 进行逆向工程。 header 由许多不同的记录组成,其中一些是可选的,我不确定顺序是否固定。
我在将 getchar() 的输入放入 char *arr[] 数组时遇到问题。我这样做的原因是因为输入数据(将是一个带有命令行参数的文件)将存储在一个 char 指针数组中以传递给 execvp 函
通常我们不能约束类型参数 T派生自密封类型(例如 struct 类型)。这将毫无意义,因为只有一种类型适合,因此不需要泛型。所以约束如下: where T : string 或: where T :
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
#include using namespace std; class A { private: int m_i; friend int main(int argc, char cons
这个问题在这里已经有了答案: Are there legitimate uses for JavaScript's "with" statement? (33 个答案) 关闭 9 年前。 我有这个代
在this answer我看到了下一个 Bash 结构。 yes "$(< file.txt)" 什么意思 "$(< file.txt)" ? 我明白了 命令替换 - $(command)用命令的结
if (a == 1) //do something else if (a == 2) //do something else if (a == 3) //do somethi
关于构造的快速简单的问题。 我有以下用于将项目添加到 ListView 的代码。 ListViewItem item = new ListViewItem(); item.Text = file; i
我想使用 std::vector 来控制给定的内存。首先,我很确定这不是好的做法,但好奇心占了上风,无论如何我都想知道如何做到这一点。 我遇到的问题是这样的方法: vector getRow(unsi
下面显示了一段简单的javascript: var mystring = ("random","ignored","text","h") + ("ello world") 这个字符串会生成 hello
在 Java 中,创建对象的标准方法是使用 MyClass name = new MyClass(); 我也经常看到构造 new MyClass() { /*stuff goes in here*/
我正在编写 C++ ndarray 类。我需要动态大小和编译时大小已知的数组(分别分配自由存储和分配堆栈)。我想支持从嵌套的 std::initializer_list 进行初始化。 动态大小的没问题
我正在将一个项目从 Visual Studio 2005 转换为 Visual Studio 2008,并提出了上述结构。 using Castle.Core.Resource; using Cast
我想知道我在这里的想法是否正确,我主要针对接口(interface)进行编程,所以我想知道下面的类是否应该通过 DI 注入(inject),或者我应该自己实例化一个类... 注意:这些服务保存在我的核
我是一名优秀的程序员,十分优秀!