【发布时间】:2017-08-16 22:52:50
【问题描述】:
我试图给一个类型一个友好的名字作为模板类型名,因为我需要在函数的几个地方使用这个名字。类型是根据参数包中其他模板参数的个数来推断的,如下所示:
#include <cassert>
#include <functional>
#include <type_traits>
template < typename ... TArgs, typename Functor = std::function< std::conditional_t< sizeof...(TArgs) == 0, int (), int (TArgs...) > > >
void DoStuff(const Functor & func, TArgs ... args) {
if constexpr (sizeof...(TArgs) == 0)
assert(typeid(Functor).hash_code() == typeid(std::function<int ()>).hash_code());
else
assert(typeid(Functor).hash_code() == typeid(std::function<int (TArgs...)>).hash_code());
}
int main(int argc, char * argv[]) {
DoStuff([] () { return 5; });
DoStuff([] (int a) { return a; });
return 0;
}
这编译得很好,但是两个断言都失败了,因为别名 Functor 实际上不是 std::function<>。另一方面,如果我更改代码以在 typeid 调用中重复 Functor 的定义,它会完美运行,如下所示:
#include <cassert>
#include <functional>
#include <type_traits>
template < typename ... TArgs, typename Functor = std::function< std::conditional_t< sizeof...(TArgs) == 0, int (), int (TArgs...) > > >
void DoStuff(const Functor & func, TArgs ... args) {
if constexpr (sizeof...(TArgs) == 0)
assert(typeid(std::function< std::conditional_t< sizeof...(TArgs) == 0, int (), int (TArgs...) > >).hash_code() == typeid(std::function<int ()>).hash_code());
else
assert(typeid(std::function< std::conditional_t< sizeof...(TArgs) == 0, int (), int (TArgs...) > >).hash_code() == typeid(std::function<int (TArgs...)>).hash_code());
}
int main(int argc, char * argv[]) {
DoStuff([] () { return 5; });
DoStuff([] (int a) { return a; });
return 0;
}
为什么第一个声明(使用typname Functor = ...)不正确?有没有不同的方法来制作这个别名?注意,在回答第二个问题时,如果解是 const 表达式就可以了,只是例子不是constexpr,因为使用了typeid。
【问题讨论】:
-
@Jarod42 仍然失败了两个断言:/
-
您要解决的实际问题是什么?为什么你认为你需要这个别名?
-
@Barry 我想消除在我需要的 3 个地方重复定义的需要。我宁愿定义一次并在其他任何地方使用别名
-
@Howard 的定义是什么?为了什么?为了做什么? 实际问题是什么?很容易回答为什么您的断言失败,但我什至不知道它们为什么在那里以及您要测试什么,所以我无法真正回答如何解决它。
-
我不是要求另一个解决方案。我只是问它的写作方式有什么问题。为什么这不是我所期望的?令我感到惊讶的是,它并没有像写的那样工作,我想知道为什么。