我有一个类型的变量std::string.我想检查它是否包含某些内容std::string.我该怎么办?
是否有一个函数在找到字符串时返回true,如果不是则返回false?
C++ 0x显示了一个使用示例std::forward:
template<class T>
void foo(T&& arg)
{
bar(std::forward<T>(arg));
}
Run Code Online (Sandbox Code Playgroud)
什么时候使用它std::forward总是有利的?
另外,它需要&&在参数声明中使用,它在所有情况下都有效吗?如果函数在其中声明,我认为你必须将临时函数传递给函数&&,那么可以使用任何参数调用foo吗?
最后,如果我有一个函数调用,如下所示:
template<int val, typename... Params>
void doSomething(Params... args) {
doSomethingElse<val, Params...>(args...);
}
Run Code Online (Sandbox Code Playgroud)
我应该用它代替:
template<int val, typename... Params>
void doSomething(Params&&... args) {
doSomethingElse<val, Params...>(std::forward<Params>(args)...);
}
Run Code Online (Sandbox Code Playgroud)
另外,如果在函数中使用两次参数,即同时转发到两个函数,是否明智使用std::forward?不会std::forward将相同的东西转换为临时两次,移动内存并使其无效以供第二次使用?以下代码是否可以:
template<int val, typename... Params>
void doSomething(Params&&... args) {
doSomethingElse<val, Params...>(std::forward<Params>(args)...);
doSomethingWeird<val, Params...>(std::forward<Params>(args)...);
}
Run Code Online (Sandbox Code Playgroud)
我有点困惑std::forward,我很乐意使用一些清理工作.
我发现更新操作std::set很繁琐,因为cppreference上没有这样的API .所以我现在做的是这样的:
//find element in set by iterator
Element copy = *iterator;
... // update member value on copy, varies
Set.erase(iterator);
Set.insert(copy);
Run Code Online (Sandbox Code Playgroud)
基本上迭代器返回的Set是a const_iterator,你不能直接改变它的值.
有一个更好的方法吗?或者也许我应该std::set通过创建我自己的(我不知道它是如何工作的...)来覆盖.
我刚刚读了std :: for_each的代码:
template<class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function f)
{
for ( ; first!=last; ++first ) f(*first);
return f;
}
Run Code Online (Sandbox Code Playgroud)
并且无法看到此模板函数返回输入函数的任何充分理由.有没有人有任何关于这将有用的例子?
在这个例子中:
import java.util.*;
public class Example {
static void doesntCompile(Map<Integer, List<? extends Number>> map) {}
static <T extends Number> void compiles(Map<Integer, List<T>> map) {}
static void function(List<? extends Number> outer)
{
doesntCompile(new HashMap<Integer, List<Integer>>());
compiles(new HashMap<Integer, List<Integer>>());
}
}
Run Code Online (Sandbox Code Playgroud)
doesntCompile() 无法编译:
Example.java:9: error: incompatible types: HashMap<Integer,List<Integer>> cannot be converted to Map<Integer,List<? extends Number>>
doesntCompile(new HashMap<Integer, List<Integer>>());
^
Run Code Online (Sandbox Code Playgroud)
whilecompiles()被编译器接受。
这个答案解释了唯一的区别是不同<? ...>,<T ...>让您稍后引用类型,这似乎并非如此。
是什么区别<? extends Number>,并<T extends Number>在这种情况下,为什么不第一编译?
静态局部变量在第一个函数调用时初始化:
在块作用域中使用指定符static声明的变量具有静态存储持续时间,但在控件第一次通过其声明时初始化(除非它们的初始化为零或常量初始化,这可以在首次输入块之前执行).在所有进一步的调用中,将跳过声明.
此外,在C++ 11中还有更多检查:
如果多个线程同时尝试初始化相同的静态局部变量,则初始化只发生一次(使用std :: call_once可以获得类似的任意函数行为).注意:此功能的常规实现使用双重检查锁定模式的变体,这可以将已初始化的局部静态的运行时开销减少到单个非原子布尔比较.(自C++ 11以来)
同时,全局变量似乎在程序启动时初始化(尽管技术上只在cppreference上提到了分配/解除分配):
静态存储时间.程序开始时分配对象的存储空间,程序结束时分配存储空间.只存在一个对象实例.在命名空间范围(包括全局命名空间)声明的所有对象都具有此存储持续时间,以及使用static或extern声明的持续时间
所以给出以下示例:
struct A {
// complex type...
};
const A& f()
{
static A local{};
return local;
}
A global{};
const A& g()
{
return global;
}
Run Code Online (Sandbox Code Playgroud)
我是否正确地假设f()每次调用它时必须检查其变量是否已初始化,因此f()会慢于g()?
我似乎无法想到一个可靠的方法(也压缩内存)从std :: vector中删除前N个元素.怎么会这样做呢?
这个答案描述了如何流式传输独立的std::variant.但是,它std::variant存储在一个时似乎不起作用std::unordered_map.
以下示例:
#include <iostream>
#include <string>
#include <variant>
#include <complex>
#include <unordered_map>
// https://stackoverflow.com/a/46893057/8414561
template<typename... Ts>
std::ostream& operator<<(std::ostream& os, const std::variant<Ts...>& v)
{
std::visit([&os](auto&& arg) {
os << arg;
}, v);
return os;
}
int main()
{
using namespace std::complex_literals;
std::unordered_map<int, std::variant<int, std::string, double, std::complex<double>>> map{
{0, 4},
{1, "hello"},
{2, 3.14},
{3, 2. + 3i}
};
for (const auto& [key, value] : map)
std::cout << key << "=" << value …Run Code Online (Sandbox Code Playgroud) 为什么在下面的代码(包括gcc 7.2和clang 4.0)Base中继承(class B)的情况下,移动构造函数是强制性的?我希望在C++ 17中不需要保证复制省略,就像composition(class A)一样.
struct Base {
Base(Base&&) = delete;
Base& operator=(Base&&) = delete;
Base()
{
}
};
Base make_base()
{
return Base{};
}
struct A {
A() : b(make_base()) {} // <<<--- compiles fine
Base b;
};
#ifdef FAIL
struct B : public Base {
B() : Base(make_base()) {} // <<<--- "Base(Base&&) is deleted"
};
#endif
Run Code Online (Sandbox Code Playgroud)
我在一些代码中遇到了一个意想不到的问题,我现在正在编写,我不确定哪个编译器是正确的.
我们有一个多参数构造函数const char*, const char*,但它被声明为显式:
constexpr explicit Wrapper(const char* a, const char* b) : pair(a,b){}
Run Code Online (Sandbox Code Playgroud)
然后我们有一个功能,需要Wrapper和一个过载,需要一个std::pair<const char*, const char*>
void q(Wrapper w);
void q(std::pair<const char *, const char *> w);
Run Code Online (Sandbox Code Playgroud)
然后我们有这样的代码,我希望将其称为第二个重载:
q({"a", "b"});
Run Code Online (Sandbox Code Playgroud)
这在clang上编译很好,但是无法在GCC和MSVC上编译.我一直试图在标准中寻找任何明确的多arg构造函数的提及,如果有什么提到这种歧义,但我没有找到相关的文本.我只是想知道哪种行为是正确的,哪种是错的?
godbolt链接:https://godbolt.org/g/2aYUov