变量模板实用程序命令无法编译

变量模板实用程序命令无法编译

本文介绍了Type_traits *_v 变量模板实用程序命令无法编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看过这个答案后,我试图想出一个变量模板实用程序来来自它的代码:

template 类模板>struct is_specialization : std::false_type {};模板<模板<类...>类模板,类... Args>struct is_specialization, Template>: std::true_type {};

并像这样实现它:

template ::value;

因为这就是我在 的头文件中看到的类似实用程序的实现方式.例如:

template 内联 constexpr bool is_same_v = is_same<_Tp, _Up>::value;模板 <typename _Base, typename _Derived>内联 constexpr bool is_base_of_v = is_base_of::value;模板 <typename _From, typename _To>内联 constexpr bool is_convertible_v = is_convertible::value;

等等.

但问题是,虽然 ::value 语法似乎有效,但 *_v 语法不起作用:

int main() {bool foo = is_specialization, std::vector>::value;//没问题bool bar = is_specialization_v, std::vector>;//编译错误!std::cout <<foo<

这会产生以下错误:

error: type/value mismatch at parameter 1 in argument 1 in the template parameter list for 'templateconstexpr const bool is_specialization_v'bool bar = is_specialization_v, std::vector>;//编译错误!^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~注意:需要一个类模板,得到 'std::vector'错误:模板<模板<类...>的模板参数列表中参数2的类型/值不匹配类模板,类...Args>constexpr const bool is_specialization_v'期望一种类型,得到向量"

这是为什么?老实说,这个错误对我来说似乎很公平,让我思考上述答案中的代码究竟如何起作用.它需要一个 class 模板 和一个参数包,但它给了 一个 class,然后是一个包.另一方面,它专注于这两种类型的组合,这让我有点困惑.我想知道:

  • 这里的扣分流程是什么?链接的答案没有详细说明代码的工作原理.
  • 在这种情况下如何引入 *_v 变量模板实用程序?

解决方案

来对比一下变量的模板参数...

template ::value;

论据

is_specialization_v, std::vector>

你声明它首先接受一个模板,然后你传递一个类型.然后你声明它接受一个类型包,但现在你传递了一个模板.问题是你感到困惑并实现了变量,因为你对主要特征进行了专门化.它不接受参数作为参数传递以放置在专业化中.它需要接受与主相同的参数,然后转发它们:

template 类模板>constexpr bool is_specialization_v = is_specialization::value;

Having seen this answer, I tried to come up with a variable template utility to the code from it:

template <class T, template <class...> class Template>
struct is_specialization : std::false_type {};

template <template <class...> class Template, class... Args>
struct is_specialization<Template<Args...>, Template> : std::true_type {};

And implement it like so:

template <template <class...> class Template, class... Args>
constexpr bool is_specialization_v = is_specialization<Template<Args...>, Template>::value;

because that's how I saw similar utilities to be implemented in the header file of <type_traits>. E.g.:

template <typename _Tp, typename _Up>
  inline constexpr bool is_same_v = is_same<_Tp, _Up>::value;
template <typename _Base, typename _Derived>
  inline constexpr bool is_base_of_v = is_base_of<_Base, _Derived>::value;
template <typename _From, typename _To>
  inline constexpr bool is_convertible_v = is_convertible<_From, _To>::value;

etc.

But the problem is that, while the ::value syntax seems to work, the *_v syntax does not:

int main() {
    bool foo = is_specialization<std::vector<int>, std::vector>::value; // No problem
    bool bar = is_specialization_v<std::vector<int>, std::vector>;      // compilation error!

    std::cout << foo << ' ' << bar;
}

This produces the following error:

Why is that? To be honest, the error seems fair to me and got me thinking how exactly does the code in the aforementioned answer work. It expects a class template and a parameter pack, but it's given a class, then a pack. On the other hand, it's specialised on the combination of those two types and it confuses me slightly. I would like to know:

  • What is the deduction process here? The linked answer does not go in detail on how the code works.
  • How to introduce the *_v variable template utility in this case?

解决方案

Let's compare the template parameters of the varaible...

template <template <class...> class Template, class... Args>
constexpr bool is_specialization_v = is_specialization<Template<Args...>, Template>::value;

to the arguments

is_specialization_v<std::vector<int>, std::vector>

You declared it to accepts first, a template, but then you pass a type. Then you declared it to accept a type pack, but now you pass a template. The problem is that you got confused and implement the variable as one does a specialization of the primary trait. It doesn't accept parameter to pass as arguments to place in the specialization. It needs to accept the same parameters as the primary, and just forward them:

template <class T, template <class...> class Template>
constexpr bool is_specialization_v = is_specialization<T, Template>::value;

这篇关于Type_traits *_v 变量模板实用程序命令无法编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 18:41