Tag: c ++ 11

std :: unique_ptr,用于需要空闲的C函数

思考一个C函数返回的东西必须是free ,例如POSIX的strdup() 。 我想在C ++ 11中使用这个函数,并避免任何泄漏的机会,这是一个正确的方法吗? #include <memory> #include <iostream> #include <string.h> int main() { char const* t { "Hi stackoverflow!" }; std::unique_ptr<char, void(*)(void*)> t_copy { strdup(t), std::free }; std::cout << t_copy.get() << " <- this is the copy!" <<std::endl; } 假设它是有道理的,有可能使用非指针类似的模式? 例如对于POSIX的函数open ,返回一个int ?

将std :: __ cxx11 :: string转换为std :: string

我使用c + + 11,但也有一些没有configuration它的库,需要一些types转换。 特别是我需要一种方法来将std::__cxx11::string转换为常规的std::string ,但是使用谷歌search我找不到一个方法来做到这一点,把(string)放在前面不起作用。 如果我不转换我得到像这样的链接器错误: undefined reference to `H5::CompType::insertMember(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned long, H5::DataType const&) const'

c ++中的扩展方法

我在c ++中寻找一个扩展方法的实现,并且在这个comp.std.c ++的讨论中提到了polymorphic_map可以被用来和一个类关联的方法,但是提供的链接似乎是死的。 有人知道这个答案是指什么,或者如果有另一种方式扩展类的扩展方法类似的方式(也许通过一些使用mixin?)。 我知道规范的C ++解决scheme是使用自由函数; 这比其他任何事情都好奇。

使用<random>的C ++中的随机数字顺序

我有下面的代码,我写了testing一个更大的程序的一部分: #include <fstream> #include <random> #include <iostream> using namespace std ; int main() { mt19937_64 Generator(12187) ; mt19937_64 Generator2(12187) ; uniform_int_distribution<int> D1(1,6) ; cout << D1(Generator) << " " ; cout << D1(Generator) << " " << D1(Generator) << endl ; cout << D1(Generator2) << " " << D1(Generator2) << " " << D1(Generator2) << […]

是否有一些忍者诀窍,使其声明后variables常量?

我知道答案是99.99%不,但我认为这是值得一试,你永远不知道。 void SomeFunction(int a) { // Here some processing happens on a, for example: a *= 50; a %= 10; if(example()) a = 0; // From this point on I want to make "a" const; I don't want to allow // any code past this comment to modify it in any way. } 我可以做一些有点类似const int […]

如何将多个int一次传入vector?

目前,我必须多次使用vector.push_back() 。 我目前使用的代码是 std::vector<int> TestVector; TestVector.push_back(2); TestVector.push_back(5); TestVector.push_back(8); TestVector.push_back(11); TestVector.push_back(14); 有没有办法只使用vector.push_back()一次,只是将多个值传入向量?

为什么Visual Studio在这种情况下不执行返回值优化(RVO)

我正在回答一个问题,并build议按值返回一个大的types,因为我确信编译器会执行返回值优化(RVO) 。 但后来有人向我指出,Visual Studio 2013在我的代码上没有执行RVO。 我在这里发现了一个关于Visual Studio未能执行RVO的问题,但是在这种情况下,结论似乎是,如果真的很重要,Visual Studio将执行RVO。 在我的情况下,它确实很重要,它对性能的影响很大,我已经通过性能分析结果进行了确认。 这是简化的代码: #include <vector> #include <numeric> #include <iostream> struct Foo { std::vector<double> v; Foo(std::vector<double> _v) : v(std::move(_v)) {} }; Foo getBigFoo() { std::vector<double> v(1000000); std::iota(v.begin(), v.end(), 0); // Fill vector with non-trivial data return Foo(std::move(v)); // Expecting RVO to happen here. } int main() { std::cout << […]

可变参数模板的部分专业化

考虑以下类模板“X”及其部分专业化。 template <class …Types> struct X {}; // #1 template <class T1> struct X<T1> {}; // #2 template <class T1, class …Types> struct X<T1, Types…> {}; // #3 X<int> x; // #2 or #3 ? 我怀疑X <int>是不明确的。 这是因为: 很明显,#2和#3比现在比较#1,#2和#3更专业。 根据14.5.5.2,让我们考虑以下#2和#3中哪一个更专业。 template <class T1> void f(X<T1>); // #2' template <class T1, class …Types> void f(X<T1, Types…>); […]

如果nullptr_t不是关键字,为什么char16_t和char32_t?

正如为什么是nullptr_t不是关键字所讨论的那样,最好避免引入新的关键字,因为它们可能会破坏向后兼容性。 那么为什么char16_t和char32_t关键字可以像这样定义呢? namespace std { typedef decltype(u'q') char16_t; typedef decltype(U'q') char32_t; }

为什么'可变'是一个lambda函数属性,而不是一个捕获types?

为什么c ++ 11要求我们写: [a,b]() mutable { a=7; } // b is needlessly mutable, potential source of bugs 代替: [mutable a,b]() { a=7; } // no problems here 这是一个疏忽,认为不够重要,还是有特定的技术原因?