小编LoS*_*LoS的帖子

为什么 std::vector::swap 具有与所有其他容器交换函数不同的 noexcept 规范?

我注意到std::vector容器的交换函数具有与所有其他容器不同的 noexcept 规范。具体来说,如果表达式 std::allocator_traits<Allocator>::propagate_on_container_swap || std::allocator_traits<Allocator>::is_always_equal为 true,则该函数为 no except,但其他容器要求表达式std::allocator_traits<Allocator>::is_always_equal为 true。

既然交换函数的行为是相同的,为什么 noexcept 规范仅在std::vector容器中不同?

c++ swap stdvector noexcept c++17

23
推荐指数
0
解决办法
827
查看次数

节点句柄与 std::unique_ptr

阅读节点句柄的文档,我注意到节点句柄类型的许多功能可以简单地通过std::unique_ptr. 事实上,节点句柄类型的功能与std::unique_ptr. 它只有一个更符合关联容器特性的接口,例如key_typemapped_type别名以及获取键/映射值引用的函数。

std::unique_ptr因此,与as的简单特化相比,该标准引入了节点句柄类型,还有其他优点吗node_type

c++ unordered-map stdmap unique-ptr c++17

2
推荐指数
1
解决办法
127
查看次数

如何将 std::sort 与 std::array 一起使用?

我需要对 an 进行排序std::array,但我不知道如何使用该函数来执行此std::sort()操作,因为我收到错误“没有重载函数排序的实例与参数列表匹配”和“没有运算符 '+' 与这些操作数匹配”。当我使用相同的语法尝试对常规数组进行排序时,没有出现任何错误。这是产生问题的代码:

#include <algorithm>
#include <array>

int nums[5] = {1, 2, 3, 4, 5};
std::array<int, 5> morenums = {1, 2, 3, 4, 5};


int main(){
    std::sort(nums, nums + 5);//no error
    std::sort(morenums, morenums + 5);//error    
}
Run Code Online (Sandbox Code Playgroud)

我试图使用该std::sort()方法对 an 进行排序std::array,但我不知道如何执行此操作,因为我不断收到错误。

c++ sorting stdarray

2
推荐指数
1
解决办法
230
查看次数

检查类模板实例化是否属于同一类模板

如何检查两个类模板实例化是否属于同一个类模板。这是我的代码

#include <iostream>
#include <type_traits>

template<typename T1, typename T2>
class A {
    float val;
public:

};

int main() {
    A<double, double> a_float_type;
    A<int, int> a_int_type;

    // how to check whether both a_double_type and a_int_type were instantiated from the same template class A<,>
    std::cout << std::is_same<decltype(a_float_type), decltype(a_int_type)>::value << std::endl; // returns false
}
Run Code Online (Sandbox Code Playgroud)

我的编译器只支持C++11

c++ type-traits c++11 is-same

2
推荐指数
1
解决办法
91
查看次数

为什么clear()函数是noexcept而析构函数不是?

我注意到,在分配器感知的容器接口中,标准要求clear()成员函数是noexcept,而析构函数不是。std::forward_list此外,在某些基于节点的容器(例如和 )的情况下会出现差异,std::set因为clear()成员函数执行与析构函数相同的操作。这种差异有什么原因吗?

c++ std noexcept c++11

1
推荐指数
1
解决办法
141
查看次数

C++17 为什么传入参数时没有提供默认构造函数

我有这门课:

\n
class MyClass {\npublic:\n    int someMember;\n    std::vector<int> someVectorMember;\n};\n
Run Code Online (Sandbox Code Playgroud)\n

我想知道为什么这行不通:

\n
MyClass(1, std::vector<int>{1,2});\n
Run Code Online (Sandbox Code Playgroud)\n

错误:

\n
main.cpp:21:37: error: no matching function for call to \xe2\x80\x98MyClass::MyClass(int, std::vector)\xe2\x80\x99\n
Run Code Online (Sandbox Code Playgroud)\n

c++ constructor initialization

1
推荐指数
1
解决办法
126
查看次数