Tag: c ++ 11

为什么不能在C ++ 0x模式下使用libc ++连接这个boost :: program_options例子?

编译boost :: program_options的示例代码: http : //svn.boost.org/svn/boost/trunk/libs/program_options/example/first.cpp …在MacOS Lion(10.7.2)上,使用安装了MacPorts的boost-1.48.0: $ clang++ -v Apple clang version 3.0 (tags/Apple/clang-211.12) (based on LLVM 3.0svn) Target: x86_64-apple-darwin11.2.0 Thread model: posix $ clang++ -std=c++0x –stdlib=libc++ -lc++ -I/opt/local/include -L/opt/local/lib -lboost_program_options first.cpp -o first Undefined symbols for architecture x86_64: "boost::program_options::options_description::options_description(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned int, unsigned int)", referenced from: _main in cc-6QQcwm.o […]

虚函数可以有默认参数吗?

如果我声明了一个基类(或接口类)并为其一个或多个参数指定了默认值,那么派生类是否必须指定相同的默认值,如果不是,哪些默认值将显示在派生类中? 附录:我也对如何在不同编译器中处理这个问题感兴趣,以及在这种情况下对“推荐”实践的任何意见。

为什么C ++ 0x的lambda需要“可变”关键字来进行按值捕获,默认情况下?

简短的例子: #include <iostream> int main() { int n; [&](){n = 10;}(); // OK [=]() mutable {n = 20;}(); // OK // [=](){n = 10;}(); // Error: a by-value capture cannot be modified in a non-mutable lambda std::cout << n << "\n"; // "10" } 问题:为什么我们需要mutable关键字? 与传统的parameter passing给命名函数完全不同。 背后的理由是什么? 我的印象是,按价值计算的全部重点是允许用户改变临时的 – 否则我几乎总是使用通过引用来获得更好的效果,不是吗? 任何启示? (我正在使用MSVC2010.AFAIK这应该是标准的)

什么是正确的方式使用C + + 11的基于范围的?

什么是正确的方式使用C + + 11的基于范围的? 应该使用什么语法? for (auto elem : container)还是for (auto& elem : container)或for (const auto& elem : container) ? 还是其他的?

什么时候应该在C ++ 11中使用constexprfunction?

在我看来,有一个“总是返回5的函数”正在打破或淡化“调用函数”的含义。 必须有一个原因,或需要这个能力,否则它不会在C ++ 11中。 为什么呢? // preprocessor. #define MEANING_OF_LIFE 42 // constants: const int MeaningOfLife = 42; // constexpr-function: constexpr int MeaningOfLife () { return 42; } 在我看来,如果我写了一个返回一个字面值的函数,然后我开始进行代码审查,那么有人会告诉我,我应该声明一个常量值而不是写返回值5。

为什么传递给线程函数的对象引用参数不能编译?

我遇到了使用新的c ++ 11 std::thread接口的问题。 我不知道如何将一个std::ostream的引用传递给线程将执行的函数。 下面是一个传递整数的例子(在gcc 4.6下按照预期编译和工作): void foo(int &i) { /** do something with i **/ std::cout << i << std::endl; } int k = 10; std::thread t(foo, k); 但是当我尝试传递一个ostream它不会编译: void foo(std::ostream &os) { /** do something with os **/ os << "This should be printed to os" << std::endl; } std::thread t(foo, std::cout); […]

将constexpr标准库函数视为constexpr是否符合编译器扩展?

gcc编译下面的代码而没有警告: #include <cmath> struct foo { static constexpr double a = std::cos(3.); static constexpr double c = std::exp(3.); static constexpr double d = std::log(3.); static constexpr double e1 = std::asin(1.); static constexpr double h = std::sqrt(.1); static constexpr double p = std::pow(1.3,-0.75); }; int main() { } 上面使用的标准库函数都不是constexpr函数 ,我们可以在C ++ 11标准 草案和C ++ 14标准草案 7.1.5 […]

C ++ 11使用非静态成员初始值设定项对类进行聚合初始化

标准允许吗? struct A { int a = 3; int b = 3; }; A a{0,1}; // ??? 这个类是聚合的吗? clang接受这个代码,但gcc不。

SFINAE工作返回types,但不作为模板参数

我已经使用了SFINAE成语很多次了,我习惯于将std::enable_if<>放在模板参数中,而不是返回types中。 然而,我遇到了一个不起作用的小事,我不知道为什么。 首先,这是我的主要: int main() { foo(5); foo(3.4); } 这里是触发错误的foo的实现: template<typename T, typename = typename std::enable_if<std::is_integral<T>::value>::type> auto foo(T) -> void { std::cout << "I'm an integer!\n"; } template<typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> auto foo(T) -> void { std::cout << "I'm a floating point number!\n"; } 这是一个可以正常工作的代码: template<typename T> auto foo(T) -> typename std::enable_if<std::is_integral<T>::value>::type { […]

为什么没有std :: shared_ptr <T >专精?

该标准提供了std::unique_ptr的模板专门化,它从析构函数中正确地调用了delete[] : void func() { std::unique_ptr< int[] > arr(new int[10]); ……. } 使用std::shared_ptr这个专门化是不可用的,所以有必要提供一个正确调用delete[] : void func() { // Usage shared_ptr array (new double [256], [](double* arr) { delete [] arr; } ); ………….. } 这只是一个疏忽吗? (以同样的方式,有一个std::copy_if )或有一个原因?