我想要实现以下目标:
template<template<typename> bool Function_, typename ... Types_>
constexpr auto find(Tuple<Types_ ... >) noexcept
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
可能的功能可能是:
template<typename T>
inline constexpr bool is_pointer_v = is_pointer<T>::value;
Run Code Online (Sandbox Code Playgroud)
那么find的用法是:
Tuple<int, char, void *> t;
find<is_pointer_v>(t);
Run Code Online (Sandbox Code Playgroud)
不要担心find的实现,我只是询问如何做" template < typename > bool Function_"因为bool当前c ++中的部分无效.
任何帮助表示赞赏!
编辑:
这是一个为什么我不能将" is_pointer" 传递给函数的例子:
template<typename T_>
constexpr auto add_pointer(Type<T_>) noexcept
{ return type_c<T_ *>; }
template<typename F_, typename T_>
constexpr auto apply(F_ f, Type<T_> t) noexcept
{
return f(t);
}
int main(void)
{
Type<int> …Run Code Online (Sandbox Code Playgroud) 我试图使用新的c ++ 17类模板推导,它似乎工作正常,直到我应用const.这是我遇到的麻烦的一个小例子:
#include <type_traits>
template <typename T>
struct X
{
T _data;
X(void) = default;
X(T && data) : _data{ data } {}
constexpr bool const_x(void) { return false; }
constexpr bool const_x(void) const { return true; }
};
template <typename T>
X(T &&) -> X<std::remove_reference_t<T>>;
int main(void)
{
X<int> a;
const X<int> b{};
X c{ 10 };
const X d{ 10 };
static_assert(!a.const_x());
static_assert(b.const_x());
static_assert(!c.const_x());
static_assert(d.const_x()); // assert fails
}
Run Code Online (Sandbox Code Playgroud)
似乎当const X推导出它的类型时,const-ness不会被执行.我知道这是可能的:
template <typename T>
X(T &&) -> …Run Code Online (Sandbox Code Playgroud) 我试图实现以下(使用C++ 17功能):
#include <type_traits>
template<auto value_>
using constant_t = std::integral_constant<decltype(value_), value_>;
template<typename ... > class Tuple {};
template<auto ... values_>
class Tuple<constant_t<values_> ... > {};
int main(void)
{
Tuple<int, int, char> types;
Tuple<1, 2, 3> values;
}
Run Code Online (Sandbox Code Playgroud)
这给了我g ++ - 7.1.0中的以下错误
main.cpp: In function ‘int main()’:
main.cpp:15:18: error: type/value mismatch at argument 1 in template parameter list for ‘template<class ...> class Tuple’
Tuple<1, 2, 3> values;
^
main.cpp:15:18: note: expected a type, got ‘1’
main.cpp:15:18: error: type/value mismatch at argument …Run Code Online (Sandbox Code Playgroud)