gpt4 book ai didi

c++ - 元编程技巧 : how to simplify implementation of two metafunctions

转载 作者:可可西里 更新时间:2023-11-01 16:38:38 25 4
gpt4 key购买 nike

我正在编写一些程序,通过代码生成自动调用一些 API。
在某些情况下,我需要将 Source 类型转换为 Target 类型,但这些类型都装饰有指针、const 等。所以我需要做的是删除所有装饰,如指针、常量、数组等,获取普通类型以将其映射到另一个类型,稍后,将装饰应用回新类型。

该实现有很多模板特化。代码后的问题。我不能使用 constexpr 元编程,因为我需要让它与 VS2013 一起工作。

template <class T>
struct TypeIs {
using type = T;
};

template <class T>
struct GetPlainType : TypeIs<typename std::decay<T>::type> {};

template <class T>
struct GetPlainType<T&> : TypeIs<typename GetPlainType<T>::type> {};

template <class T>
struct GetPlainType<T const &> : TypeIs<typename GetPlainType<T>::type> {};

template <class T>
struct GetPlainType<T &&> : TypeIs<typename GetPlainType<T>::type> {};

template <class T>
struct GetPlainType<T const &&> : TypeIs<typename GetPlainType<T>::type> {};

template <class T>
struct GetPlainType<T*> : TypeIs<typename GetPlainType<T>::type> {};

template <class T>
struct GetPlainType<T const *> : TypeIs<typename GetPlainType<T>::type> {};

template <class T>
struct GetPlainType<T const> : TypeIs<typename GetPlainType<T>::type> {};

template <class T>
struct GetPlainType<T[]> : TypeIs<typename GetPlainType<T>::type> {};


template <class T>
struct GetPlainType<T const[]> : TypeIs<typename GetPlainType<T>::type> {};

template <class T, std::size_t I>
struct GetPlainType<T[I]> : TypeIs<typename GetPlainType<T>::type> {};

template <class T, std::size_t I>
struct GetPlainType<T const [I]> : TypeIs<typename GetPlainType<T>::type> {};


template <class T>
using GetPlainType_t = typename GetPlainType<T>::type;


template <class Decorated, class Plain>
struct CopyDecorations : TypeIs<Plain> {};


template <class T, class Plain>
struct CopyDecorations<T const, Plain> :
TypeIs<typename CopyDecorations<T, Plain const>::type> {};


template <class T, class Plain>
struct CopyDecorations<T *, Plain> :
TypeIs<typename CopyDecorations<T, Plain *>::type> {};

template <class T, class Plain>
struct CopyDecorations<T const *, Plain> :
TypeIs<typename CopyDecorations<T, Plain const *>::type> {};


template <class T, class Plain>
struct CopyDecorations<T &, Plain> :
TypeIs<typename CopyDecorations<T, Plain &>::type> {};

template <class T, class Plain>
struct CopyDecorations<T const &, Plain> :
TypeIs<typename CopyDecorations<T, Plain const &>::type> {};

template <class T, class Plain>
struct CopyDecorations<T &&, Plain> :
TypeIs<typename CopyDecorations<T, Plain &&>::type> {};


template <class T, class Plain>
struct CopyDecorations<T const &&, Plain> :
TypeIs<typename CopyDecorations<T, Plain const &&>::type> {};


template <class T, class Plain>
struct CopyDecorations<T[], Plain> :
TypeIs<typename CopyDecorations<T, Plain[]>::type> {};

template <class T, class Plain>
struct CopyDecorations<T const [], Plain> :
TypeIs<typename CopyDecorations<T, Plain const []>::type> {};


template <class T, class Plain, std::size_t I>
struct CopyDecorations<T [I], Plain> :
TypeIs<typename CopyDecorations<T, Plain[I]>::type> {};

template <class T, class Plain, std::size_t I>
struct CopyDecorations<T const [I], Plain> :
TypeIs<typename CopyDecorations<T, Plain const [I]>::type> {};


template <class Decorated, class Plain>
using CopyDecorations_t = typename CopyDecorations<Decorated, Plain>::type;


int main()
{
static_assert(std::is_same<GetPlainType_t<int>, int>{}, "");
static_assert(std::is_same<GetPlainType_t<int const>, int>{}, "");
static_assert(std::is_same<GetPlainType_t<int *>, int>{}, "");
static_assert(std::is_same<GetPlainType_t<int **>, int>{}, "");
static_assert(std::is_same<GetPlainType_t<int * &>, int>{}, "");
static_assert(std::is_same<GetPlainType_t<int ** &>, int>{}, "");
static_assert(std::is_same<GetPlainType_t<int const * []>, int>{}, "");
static_assert(std::is_same<GetPlainType_t<int const **[][3][5]>, int>{}, "");


static_assert(std::is_same<CopyDecorations_t<int, double>, double>{}, "");
static_assert(std::is_same<CopyDecorations_t<int const, double>, double const>{}, "");
static_assert(std::is_same<CopyDecorations_t<int *, double>, double *>{}, "");
static_assert(std::is_same<CopyDecorations_t<int **, double>, double **>{}, "");
static_assert(std::is_same<CopyDecorations_t<int[], double>, double[]>{}, "");
static_assert(std::is_same<CopyDecorations_t<int[3], double>, double[3]>{}, "");


//******************THE TESTS BELOW DO NOT WORK
//static_assert(std::is_same<CopyDecorations_t<int[][3], double>, double[][3]>{}, "");
//static_assert(std::is_same<CopyDecorations_t<int * &, double>, double * &>{}, "");
// static_assert
// (
//std::is_same<CopyDecorations_t<int const * [], double>,
// double const * []>{}, "");
// static_assert
// (std::is_same<CopyDecorations_t<int const **[][3][5], double>,
// double const **[][3][5]>{}, "");
}

问题:

  1. 我可以简化实现吗?
  2. 失败的测试(参见 main 函数),我该如何修复它们?
  3. 在哪些情况下(忽略 volatile 和指向成员的指针、指向函数的指针和函数)。你认为我的实现会失败吗?

最佳答案

我发现这个问题确实是关于 SO 上的 C++ 元编程最有趣的问题之一。
我喜欢尝试找到合适的解决方案。谢谢你。 :-)


它遵循一个最小的工作示例。
它并不完整,但它提供了一种可能的方法来实现这一点。
f 函数(好吧,你可以在你的代码中选择一个更好的名字)接受两个模板参数:要清理的类型和要装饰.
它返回一个模板类型 (types),它引入了两个 using 声明,basicdecorated,第一个模板参数清理为 basic 和第二个装饰为decorated
它一次完成所有工作(清理和装饰)。您仍然可以只使用第一个参数,在这种情况下 decorated 默认为装饰的 char 类型。

完整代码如下:

#include<type_traits>
#include<cstddef>

static constexpr std::size_t N = 42;

template<std::size_t N>
struct choice: choice<N-1> {};

template<>
struct choice<0> {};

template<typename T, typename U>
struct types {
using basic = T;
using decorated = U;
};

template<typename T, typename U>
constexpr auto
f(choice<0>) { return types<T, U>{}; }

template<typename T, typename U,
typename = std::enable_if_t<std::is_pointer<T>::value>>
constexpr auto f(choice<1>) {
auto t = f<std::remove_pointer_t<T>, U>(choice<N>{});
using B = typename decltype(t)::basic;
using D = typename decltype(t)::decorated;
return types<B, std::add_pointer_t<D>>{};
}

template<typename T, typename U,
typename = std::enable_if_t<std::is_lvalue_reference<T>::value>>
constexpr auto f(choice<2>) {
auto t = f<std::remove_reference_t<T>, U>(choice<N>{});
using B = typename decltype(t)::basic;
using D = typename decltype(t)::decorated;
return types<B, std::add_lvalue_reference_t<D>>{};
}

template<typename T, typename U,
typename = std::enable_if_t<std::is_rvalue_reference<T>::value>>
constexpr auto f(choice<3>) {
auto t = f<std::remove_reference_t<T>, U>(choice<N>{});
using B = typename decltype(t)::basic;
using D = typename decltype(t)::decorated;
return types<B, std::add_rvalue_reference_t<D>>{};
}

template<typename T, typename U,
typename = std::enable_if_t<std::is_array<T>::value>>
constexpr auto f(choice<4>) {
auto t = f<std::remove_extent_t<T>, U>(choice<N>{});
using B = typename decltype(t)::basic;
using D = typename decltype(t)::decorated;
return types<B, std::conditional_t<(0==std::extent<T>::value), D[], D[std::extent<T>::value]>>{};
}

template<typename T, typename U,
typename = std::enable_if_t<std::is_const<T>::value>>
constexpr auto f(choice<5>) {
auto t = f<std::remove_const_t<T>, U>(choice<N>{});
using B = typename decltype(t)::basic;
using D = typename decltype(t)::decorated;
return types<B, std::add_const_t<D>>{};
}

template<typename T, typename U,
typename = std::enable_if_t<std::is_volatile<T>::value>>
constexpr auto f(choice<6>) {
auto t = f<std::remove_volatile_t<T>, U>(choice<N>{});
using B = typename decltype(t)::basic;
using D = typename decltype(t)::decorated;
return types<B, std::add_volatile_t<D>>{};
}

template<typename T, typename U = char>
constexpr auto f() {
return f<T, U>(choice<N>{});
}

int main() {
// something complex to show that it seems to work
static_assert(std::is_same<
decltype(f<const int ** const &&, char>()),
types<int, const char ** const &&>
>::value, "!");

// some of the OP's examples (the most interesting)
static_assert(std::is_same<decltype(f<int, int>()), types<int, int>>::value, "!");
static_assert(std::is_same<decltype(f<int const, int>()), types<int, int const>>::value, "!");
static_assert(std::is_same<decltype(f<int *, int>()), types<int, int *>>::value, "!");
static_assert(std::is_same<decltype(f<int **, double>()), types<int, double **>>::value, "!");
static_assert(std::is_same<decltype(f<int *&, int>()), types<int, int *&>>::value, "!");
static_assert(std::is_same<decltype(f<int **&, float>()), types<int, float **&>>::value, "!");
static_assert(std::is_same<decltype(f<int [3], char>()), types<int, char [3]>>::value, "!");
static_assert(std::is_same<decltype(f<int [], int>()), types<int, int []>>::value, "!");
static_assert(std::is_same<decltype(f<int [][3], double>()), types<int, double [][3]>>::value, "!");
static_assert(std::is_same<decltype(f<int const **[][3][5], int>()), types<int, int const **[][3][5]>>::value, "!");

// of course, you don't need to provide the second type if you don't need it
// in this case, types::decorated is defaulted to a decorated char type
f<int const **[][3][5]>();
}

除了因为 static_assert 而没有 constexpr 就无法编译的事实之外,您可以自由地删除它们并在运行时使用该函数。

实际上,它可能会变成一个无定义的解决方案,为声明提供正确的返回类型并使用一堆decltype,但我怀疑它远非可读性。

编辑

正如 OP 所提到的,他不想(或者至少,他不能使用)constexprs。
它遵循了一个略有不同的解决方案,仍然基于之前的解决方案。
基本思想是将 f 用作带有 decltype 的未计算操作数。
这是完整的代码:

#include<type_traits>
#include<cstddef>

static const std::size_t N = 42;

template<std::size_t N>
struct choice: choice<N-1> {};

template<>
struct choice<0> {};

template<typename T, typename U>
struct types {
using basic = T;
using decorated = U;
};

template<typename T, typename U>
auto f(choice<0>) { return types<T, U>{}; }

template<typename T, typename U,
typename = std::enable_if_t<std::is_pointer<T>::value>>
auto f(choice<1>) {
auto t = f<std::remove_pointer_t<T>, U>(choice<N>{});
using B = typename decltype(t)::basic;
using D = typename decltype(t)::decorated;
return types<B, std::add_pointer_t<D>>{};
}

template<typename T, typename U,
typename = std::enable_if_t<std::is_lvalue_reference<T>::value>>
auto f(choice<2>) {
auto t = f<std::remove_reference_t<T>, U>(choice<N>{});
using B = typename decltype(t)::basic;
using D = typename decltype(t)::decorated;
return types<B, std::add_lvalue_reference_t<D>>{};
}

template<typename T, typename U,
typename = std::enable_if_t<std::is_rvalue_reference<T>::value>>
auto f(choice<3>) {
auto t = f<std::remove_reference_t<T>, U>(choice<N>{});
using B = typename decltype(t)::basic;
using D = typename decltype(t)::decorated;
return types<B, std::add_rvalue_reference_t<D>>{};
}

template<typename T, typename U,
typename = std::enable_if_t<std::is_array<T>::value>>
auto f(choice<4>) {
auto t = f<std::remove_extent_t<T>, U>(choice<N>{});
using B = typename decltype(t)::basic;
using D = typename decltype(t)::decorated;
return types<B, std::conditional_t<(0==std::extent<T>::value), D[], D[std::extent<T>::value]>>{};
}

template<typename T, typename U,
typename = std::enable_if_t<std::is_const<T>::value>>
auto f(choice<5>) {
auto t = f<std::remove_const_t<T>, U>(choice<N>{});
using B = typename decltype(t)::basic;
using D = typename decltype(t)::decorated;
return types<B, std::add_const_t<D>>{};
}

template<typename T, typename U,
typename = std::enable_if_t<std::is_volatile<T>::value>>
auto f(choice<6>) {
auto t = f<std::remove_volatile_t<T>, U>(choice<N>{});
using B = typename decltype(t)::basic;
using D = typename decltype(t)::decorated;
return types<B, std::add_volatile_t<D>>{};
}

template<typename T, typename U>
auto f() {
return f<T, U>(choice<N>{});
}

template<typename T, typename U = char>
using my_type = decltype(f<T, U>());

template<typename T, typename U = char>
using my_type_basic_t = typename decltype(f<T, U>())::basic;

template<typename T, typename U = char>
using my_type_decorated_t = typename decltype(f<T, U>())::decorated;

int main() {
int i = 42;
my_type_decorated_t<char *, int> ptr = &i;
// of course, it can still be used in a constant expression if needed
// constexpr my_type_decorated_t<char *, int> ptr = nullptr;

}

关于c++ - 元编程技巧 : how to simplify implementation of two metafunctions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39143903/

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