我注意到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
容器中不同?
阅读节点句柄的文档,我注意到节点句柄类型的许多功能可以简单地通过std::unique_ptr
. 事实上,节点句柄类型的功能与std::unique_ptr
. 它只有一个更符合关联容器特性的接口,例如key_type
和mapped_type
别名以及获取键/映射值引用的函数。
std::unique_ptr
因此,与as的简单特化相比,该标准引入了节点句柄类型,还有其他优点吗node_type
?
我需要对 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
,但我不知道如何执行此操作,因为我不断收到错误。
如何检查两个类模板实例化是否属于同一个类模板。这是我的代码
#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
我注意到,在分配器感知的容器接口中,标准要求clear()
成员函数是noexcept
,而析构函数不是。std::forward_list
此外,在某些基于节点的容器(例如和 )的情况下会出现差异,std::set
因为clear()
成员函数执行与析构函数相同的操作。这种差异有什么原因吗?
我有这门课:
\nclass MyClass {\npublic:\n int someMember;\n std::vector<int> someVectorMember;\n};\n
Run Code Online (Sandbox Code Playgroud)\n我想知道为什么这行不通:
\nMyClass(1, std::vector<int>{1,2});\n
Run Code Online (Sandbox Code Playgroud)\n错误:
\nmain.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++ ×6
c++11 ×2
c++17 ×2
noexcept ×2
constructor ×1
is-same ×1
sorting ×1
std ×1
stdarray ×1
stdmap ×1
stdvector ×1
swap ×1
type-traits ×1
unique-ptr ×1