c++ - 为什么我需要在以下情况下使用 std::decay?

标签 c++ templates c++14

我是C++的新手,做了一个测试程序来了解更多关于decltype的信息, std::decay , std::is_same_v (特征)还有typeid .

我有以下简单类,我想在其中获取模板参数类型 TypeBase类的构造函数使用 decltype .

喜欢decltype(content of std::vector<Type>::iterator::begin) .有些方法,它没有用。

#include <iostream>
#include <vector>
#include <type_traits>
#include <initializer_list>
#include <typeindex>

template<typename Type> class Base
{
    std::vector<Type> vec;
public :
    Base(std::initializer_list<decltype(*vec.begin())> liVec): vec(liVec) {}
    // here                    ^^^^^^^^^^^^^^^^^^^^^^^^ . isn't enough?? 
};

int main()
{
    //Base<int> obj{ 1, 2, 3, 4, 5 };     // does not works: error !

    // to see the type, I wrote the following code
    std::vector<int> vec;
    std::cout << typeid(decltype(*vec.begin())).name() << std::endl; // prints i means int type(I know not reliable though)
    // and
    std::cout << std::boolalpha << std::is_same_v<int, decltype(*vec.begin())> << std::endl; // prints false 

    return 0;
}

关于Base<int> obj{ 1, 2, 3, 4, 5 };行的错误是(在 GCC 6.1、C++14 中)

include\c++\initializer_list||In instantiation of 'class std::initializer_list<int&>':|
include\c++\initializer_list|54|error: forming pointer to reference type 'int&'|
include\c++\initializer_list|55|error: forming pointer to reference type 'int&'|
main.cpp|17|error: no matching function for call to 'Base<int>::Base(<brace-enclosed initializer list>)'|
candidate: 'Base<Type>::Base(std::initializer_list<decltype (*((Base<Type>*)(void)0)->Base<Type>::vec.begin())>) [with Type = int; decltype (*((Base<Type>*)(void)0)->Base<Type>::vec.begin()) = int&]'|
main.cpp|11|note:   candidate expects 1 argument, 5 provided|
main.cpp|7|note: candidate: 'Base<int>::Base(const Base<int>&)'|
main.cpp|7|note:   candidate expects 1 argument, 5 provided|
main.cpp|7|note: candidate: 'Base<int>::Base(Base<int>&&)'|
main.cpp|7|note:   candidate expects 1 argument, 5 provided|

正如我所见,编译器告诉我一些关于 int& 的信息,我只是通过以下方式尝试了它。(即 std::decay_t )并且它有效。

template<typename Type> class Base
{
    std::vector<Type> vec;
public :
    Base(std::initializer_list<std::decay_t<decltype(*vec.begin())>> liVec): vec(liVec) {}
                               ^^^^^^^^^^^^^^^^^^^^^^^^ why I need this here?
};

int main()
{
    Base<int> obj{ 1, 2, 3, 4, 5 };     // works now

    std::vector<int> vec;
    std::cout << typeid(decltype(*vec.begin())).name() << std::endl; // prints i means int type(not reliable though)
    // and
    std::cout << std::boolalpha << std::is_same_v<int, std::decay_t<decltype(*vec.begin())>> << std::endl; // true now: WHY?

    return 0;
}

但我不知道错误的含义以及它为什么起作用。有人可以向我解释到底发生了什么吗?

最佳答案

您的错误消息的第一行说(除其他外):

instantiation of 'class std::initializer_list<int&>'

所以它正在尝试创建一个 initializer_list 并带有不允许的引用类型

查看您的模板代码:

 std::initializer_list<decltype(*vec.begin())>

现在 vec.begin() 产生一个 iterator 并且取消引用一个 iterator 产生一个 reference 所以你可以做这样的事情:

*iter = whatever;

因此您需要删除类型的引用 部分。 std::decay_t这样做。

关于c++ - 为什么我需要在以下情况下使用 std::decay?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52352805/

相关文章:

c++ - constexpr 变量必须由 const 表达式初始化

c++ - QTableWidget 随机加载效率低下 - C++

c++ - 在 if 语句评估中 boost scoped_lock 对象

c++ - 强制编译器推出 for 循环的可能性

c++ - C++1y 中是否需要公共(public)类内类型定义?

c++ - 带命令提示符的命名管道

c++ - 是否可以在 C++ 中提取容器模板类?

c++ - 函数模板参数推导

c++ - 为什么我的 "string mingling"方法返回意外结果?

c++ - 如何将现有对象的地址分配给智能指针?