STL新手问题:
关于函数std::map::upper_bound
,std::map::lower_bound
指定一个实际上不存在于地图中的键是否有效?
例
std::map<int,int> intmap;
std::map<int,int>::iterator it1, it2;
intmap[1] = 10;
intmap[2] = 20;
intmap[4] = 40;
intmap[5] = 50;
it1 = intmap.lower_bound (3); // Is this valid?
it2 = intmap.upper_bound (3); // Is this valid?
Run Code Online (Sandbox Code Playgroud) 我对此代码段有疑问:
template <typename T>
struct S
{
static int a;
};
template <typename T>
decltype(S<T>::a) S<T>::a;
Run Code Online (Sandbox Code Playgroud)
clang-3.4
说:
s.cpp:8:25: error: redefinition of 'a' with a different type: 'decltype(S<T>::a)' vs 'int'
decltype(S<T>::a) S<T>::a;
^
s.cpp:4:14: note: previous definition is here
static int a;
^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
但gcc-4.8.2
接受.哪个编译器是对的?我将来应该避免这样的代码吗?
可以说我有类Date
和类Year
,Month
和Day
.
struct Date {
Date(Year year, Month month, Day day) : d(day), m(month), y(year) {};
Date(Month month, Day day, Year year) : d(day), m(month), y(year) {};
Date(Day day, Month month, Year year) : d(day), m(month), y(year) {};
Date(Day day, Year year, Month month) : d(day), m(month), y(year) {};
...
...
private:
Day d;
Month m;
Year y;
}
Run Code Online (Sandbox Code Playgroud)
这允许我没有特定的参数布局,Date
因为我有很多过载.
我能自动生成所有排列/过载吗?
只是要清楚:
我知道标题没有多大意义,但代码将解释我的问题。
template<typename T>
void foo(T...) {std::cout << 'A';}
template<typename... Ts>
void foo(Ts...) {std::cout << 'B';}
int main(){
foo(1);
foo(1,2);
}
Run Code Online (Sandbox Code Playgroud)
在阅读续篇之前尝试猜测这个程序的输出:
所以输出是 AB
谁能解释为什么 1 个参数函数优先考虑省略号,而 2 个参数优先考虑可变参数模板?
有人可以解释为什么一次使用方法c(T*)
和下一次d<>(int*)
?方法c
和d
我似乎完全相同,我无法弄清楚为什么不是同一类型的方法调用.
#include <iostream>
using namespace std;
template<typename T>
void c(T){ cout <<"(T)" << endl; }
template<>
void c<>(int*){ cout <<"(int*)" << endl; }
template<typename T>
void c(T*){ cout <<"(T*)" << endl; }
template<typename T>
void d(T){ cout <<"(T)" << endl; }
template<typename T>
void d(T*){ cout <<"(T*)" << endl; }
template<>
void d<>(int*){ cout <<"(int*)" << endl; }
int main(){
int i;
c(&i);
d(&i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
(T*)
(int*)
Run Code Online (Sandbox Code Playgroud) 为什么std::barrier
在堆上分配内存而不在堆上分配内存std::latch
?
它们之间的主要区别是std::barrier
可以重用而std::latch
不能,但我找不到解释为什么这会让前者分配内存。
我正在尝试移植一个库以在 MSVC 上进行编译。该库将数据存储在向量元组 ( std::tuple<std::vector<Ts>...>
) 中,并使用自定义迭代器同时迭代所有向量(类似于 zip_iterator 的作用)。
迭代器定义的类型如下所示(假设Ts...
-> <int, int>
):
`value_type` is `std::tuple<int, int>`
`reference` is `std::tuple<int&, int&>`
Run Code Online (Sandbox Code Playgroud)
问题是,在最新的 MSVC (v. 19.35) 上,这个迭代器不满足 的概念std::input_iterator
,而在 gcc/clang 上却满足它。
经过进一步调查,我发现失败是由于std::common_reference
元组上的概念行为不一致造成的。
以下static_assert
内容在 MSVC 上失败,而在 gcc/clang 上不会失败
using T = std::tuple<int, int>&;
using U = std::tuple<int&, int&>;
static_assert(std::common_reference_with<T, U>, "failed common_reference_with");
Run Code Online (Sandbox Code Playgroud)
这是Godbolt上的(还有一个迭代器示例)
像这样的类型std::tuple<int, int>&
应该有一个“ common_reference_with
”std::tuple<int&, int&>
吗?MSVC 说不,gcc 说可以。
根据 C++20 及以后的标准,这两种行为中的哪一种是预期的?
有没有什么简单的方法可以使这个迭代器成功通过 MSVC 上的迭代器概念检查(即强制这两种类型具有共同的引用)? …
template <typename ReturnType, typename... Args>
std::function<ReturnType (Args...)>
memoize(std::function<ReturnType (Args...)> func)
{
std::map<std::tuple<Args...>, ReturnType> cache;
return ([=](Args... args) mutable {
std::tuple<Args...> t(args...);
if (cache.find(t) == cache.end())
cache[t] = func(args...);
return cache[t];
});
}
Run Code Online (Sandbox Code Playgroud)
你能解释一下吗?我在这里无法理解很多东西,但最奇怪的是缓存是本地的而不是静态的,但也许我错了......
是否有可能以std::optional::value_or(expr)
懒惰的方式评估参数,所以expr
仅在没有值的情况下计算?
如果没有,什么是适当的替代品?
请考虑以下C ++程序:
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::vector<std::string> v(2, std::string(24,0));
for (auto& s : v) {
std::cout << "Address: " << (void*)s.data() << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
我希望向量中的每个字符串都指向不同的内存区域,但是在使用gcc 6.3.0和8.2.1进行编译时-D_GLIBCXX_USE_CXX11_ABI=0
,它们显示相同的地址。(在不带标志的情况下进行编译时,它们将显示不同的地址)。这是为什么?
c++ ×10
c++11 ×4
std ×2
templates ×2
c++-concepts ×1
c++17 ×1
c++20 ×1
lambda ×1
memoization ×1
overloading ×1
stdoptional ×1
stl ×1
tuples ×1