Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我需要在以下情况下使用std::Decage? 我是C++的新手,做了一个测试程序,了解了关于 DECKEDATION/COD>, STD::Reals, STD::ISSAMEYVI(MULTION),还有 Type ID < /C> > /P>_C++_Templates_C++14 - Fatal编程技术网

为什么我需要在以下情况下使用std::Decage? 我是C++的新手,做了一个测试程序,了解了关于 DECKEDATION/COD>, STD::Reals, STD::ISSAMEYVI(MULTION),还有 Type ID < /C> > /P>

为什么我需要在以下情况下使用std::Decage? 我是C++的新手,做了一个测试程序,了解了关于 DECKEDATION/COD>, STD::Reals, STD::ISSAMEYVI(MULTION),还有 Type ID < /C> > /P>,c++,templates,c++14,C++,Templates,C++14,我有一个简单的类,在这个类中,我想使用decltype在Base类的构造函数中获取模板参数typetype 比如decltype(std::vector::iterator::begin的内容)。有些人认为,这是行不通的 #include <iostream> #include <vector> #include <type_traits> #include <initializer_list> #include <typeindex>

我有一个简单的类,在这个类中,我想使用
decltype
Base
类的构造函数中获取模板参数type
type

比如
decltype(std::vector::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;
}

但我不知道这个错误的含义以及它为什么起作用。有人能告诉我到底发生了什么吗?

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


因此,您需要删除类型的引用部分。是这样的。

typeid
不关心类型的引用或常量限定符,因此即使您看到
i
,也可能是
int
const int
int&
const int&
,等等。@Holt-Oh。。这是有道理的。这样看来,
typeid
在任何情况下都不是很有用。不是吗?
typeid
提供了一个
std::type_info
,它反过来又可以提供一个
std::type_索引
,它可以用作关联容器中的键,因此可以使用它将“type”映射到您想要的任何内容。这在特定情况下可能有一些用处。@KillzoneKid
decltype
中的任何内容都是未计算的操作数,因此这不是UB。@Const
decltype
中的任何内容都不会计算,因此只要表达式在语法上有效,您就可以执行任何您想要的操作
decltype(vec)::只要您使用的类型具有
value\u type
成员(适用于所有标准容器),value\u type
应该可以工作。所以您的意思是
*vec.begin()
等于
type&
,在这种情况下???@Const Yes,如果是这样的话,
std::remove_reference
也将执行与
std::decay
相同的工作。只是想了解一下。@Const
std::decay
做了很多事情,包括像
std::remove\u reference
那样删除引用。您可以在这里阅读更多内容:,还可以查看“可能的实现”部分,了解它的实际功能。好的。我需要更多的阅读资料。与此同时,你已经澄清了我的问题。非常感谢
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|
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&>'
 std::initializer_list<decltype(*vec.begin())>
*iter = whatever;