c++11专题提供c++11的最新资讯内容,帮你更好的了解c++11。
看,我没有得到的,为什么像以下这样的程序是合法的? int main() { static const int i = 0; i < i > i; } 我的意思是,当然,没有人真正拥有任何当前具有无副作用的表达式的程序,因为这将是非常无意义的,并且它将使得解析和编译语言更容易.那么为什么不禁止它们呢?允许这种语法,语言实际上有什么好处? 另一个例子是这样的: int main()
我正在使用std :: bind来提供回调,同时通过首先绑定一些参数来抽象一些逻辑.即 void start() { int secret_id = 43534; //Bind the secret_id to the callback function object std::function<void(std::string)> cb = std::bind(&ca
这是相关代码的链接: #include <iostream> #include <string> #include <vector> #include <type_traits> int main() { std::vector<int> v{1, 2, 3, 4, 5}; auto iter = begin(std::move(v)); if(std::is_const<typen
假设我使用完美转发具有类似printf的功能(用于记录): template<typename... Arguments> void awesome_printf(std::string const& fmt, Arguments&&... args) { boost::format f(fmt); f % /* How to specify `args` here? */;
我需要为std :: vector创建一个shared_ptr,正确的语法是什么? std::vector<uint8_t> mVector; shared_ptr<std::vector<uint8_t>> mSharedPtr = &mVector; 上面的代码不能编译. 谢谢. 你要做的是让一个智能指针管理一个堆栈对象.这不起作用,因为堆栈对象在超出范围时会自行终止.智能指针不能阻止它这样做
使用std :: string的移动赋值运算符(在VC11中)需要什么? 我希望它会被自动使用,因为在任务完成后不再需要v. 在这种情况下是否需要std :: move?如果是这样,我不妨使用非C 11交换. #include <string> struct user_t { void set_name(std::string v) { name_ = v;
我正在使用Visual Studio 2012,因此C 11大部分都可以…… 提升也很好,但我宁愿避免其他的自由,至少不是威德利用过的. 我想以最优雅的方式创建一个只返回无限序列的前向迭代器.例如,所有自然数的序列. 基本上我想要这个f#代码的C等效: let nums = seq { while true do yield 1 yield
我在C中使用构造“thread”,我在递归函数中创建了一个可变数量的线程.我希望主线程等待所有这些.没有WaitForMultipleObjects我怎么能这样做? 看看cplusplus中的 example.它们在向量中存储带有push_back()的线程.最后你有连接循环. std::vector<std::thread> threads; //create threads for (int
给定一个排序的std :: vector< int>,我想使用C 11-STD函数来找到元素从负变为正的索引. 我知道我可以使用二进制搜索来实现它,但我感兴趣的是标准库中是否有任何函数,类似于unaryfind_if,这将有助于此搜索(可能与正确的lambda表达式相关). 你应该找到0的 lower_bound: auto iter = std::lower_bound(vec.begin(),
我有以下代码,其中一个变量正在使用函数调用的结果进行初始化.这个函数抛出所以我设置了一个try-catch来捕获异常.由于某种原因,即使在catch子句运行后,异常仍会出现在屏幕上. #include <iostream> #include <stdexcept> int f() { throw std::invalid_argument("threw"); return 50; } stru
我想明确关于成员变量的数组大小限制,以阻止其他人意外地进行愚蠢的更改.以下天真的尝试将无法编译: struct Foo { std::array< int, 1024 > some_array; static_assert( (some_array.size() % 256) == 0, "Size must be multiple of 256" ); //^ (clan
当仅使用标准类型参数实例化具有模板化类类型的变量时,语法如下所示: template<typename Arg = int> class Templ; Templ<>& myTempl; 省略空参数列表<>应该给出编译错误,因为需要模板参数列表. 但显然(至少在VS2013下),以下声明不需要模板参数列表: template<typename Arg> //" = int" left out c
请考虑以下代码: main() { bool t; ... std::function<bool (bool)> f = t ? [](bool b) { return b; } : [](bool b) { return !b; }; // OK std::function<bool (bool)> f = t ? [t](bool b) { return t =
当在C中进行模板元编程时,我经常会碰到以下内容: template <typename T> S<T> make_wrapper(T&& t) { return S<T>(std::forward<T>(t)); } 我知道我应该在返回类型中使用类似std :: decay的东西,但是为什么std :: remove_reference也不会工作呢?这有什么区别?那么std :: remove_c
我尝试使用不同的编译器(包括 gcc 6.1)编译以下程序: #include <optional> int main() { std::optional<int> o1; } 输出是 main.cpp:1:20: fatal error: optional: No such file or directory #include optional 对于此处给出的示例,情况甚至如此: htt
我尝试过这样的事情: std::copy(std::make_move_iterator(s1.begin()), std::make_move_iterator(s1.end()), std::make_move_iterator(s2.begin())); 并得到这个错误: error: using xvalue (rvalue reference) as lvalue
我目前正在用C编写素数生成器.我先制作了一个单线程版本,后来又制作了一个多线程版本. 我发现如果我的程序生成的值小于100’000,则单线程版本比多线程版本更快.显然我做错了什么. 我的代码如下: #include <iostream> #include <fstream> #include <set> #include <string> #include <thread> #include <m
让我们考虑以下代码片段 void Test() { int x = 0; int& rx = x; int* px = &x; auto apx = px; // deduced type is int* auto arx = rx; // deduced type is int } 可以从指针类型中得出类比,期望推导出的arx类型是int&,但实际上它
我在提到问题“ “IF” argument evaluation order?”是为了理解c中“if”语句的评估顺序. 下面是if语句中的条件以错误的顺序计算的代码. #include <iostream> using namespace std; int main() { int t = 0; if((1 / t) == 1 && t != 0) {
我在c 11中有一个关于智能指针的问题.我已经开始查看C 11(我通常在c#中编程)并阅读有关智能指针的一些内容.现在我有了问题,智能指针是否完全取代了“旧”指针的风格,我应该总是使用它们吗? unique_ptr似乎解决了C中内存管理的所有问题,还是我错了? 例如: std::unique_ptr<GameManager> game (new GameManager()); game->Sta