我是c++的初学者。我遇到了在我正在处理的头文件中使用的覆盖关键字。请问,什么是重写的真正用途,也许用一个例子就容易理解了。

c++ 11向量有了新函数emplace_back。与依赖编译器优化来避免复制的push_back不同,emplace_back使用完全转发将参数直接发送给构造函数以就地创建对象。在我看来,emplace_back做了所有push_back能做的事情,但有时它会做得更好(但不会更差)。

我使用push_back的原因是什么?

我问这个问题,首先不是因为垃圾收集的优点。我问这个问题的主要原因是我知道Bjarne Stroustrup说过c++在某个时间点上会有一个垃圾收集器。

话虽如此,为什么还没有加入呢?c++已经有了一些垃圾收集器。这是那种“说起来容易做起来难”的事情吗?或者还有其他原因没有添加它(并且不会在c++ 11中添加)?

交叉链接:

c++的垃圾收集器

澄清一下,我理解c++最初创建时没有垃圾收集器的原因。我想知道为什么不能添加收集器。

c++代码是否可能同时符合c++ 03标准和c++ 11标准,但根据编译的标准做不同的事情?

Std::unique_ptr支持数组,例如:

std::unique_ptr<int[]> p(new int[10]);

但这是必要的吗?可能使用std::vector或std::array更方便。

你觉得这个结构有什么用处吗?

我有一个.ps1文件,我想在其中定义自定义函数。

假设文件名为MyFunctions。Ps1,内容如下:

Write-Host "Installing functions"
function A1
{
    Write-Host "A1 is running!"
}
Write-Host "Done"

为了运行这个脚本并理论上注册A1函数,我导航到.ps1文件所在的文件夹并运行该文件:

.\MyFunctions.ps1

这个输出:

Installing functions
Done

然而,当我尝试调用A1时,我只是得到一个错误,说明没有该名称的命令/函数:

The term 'A1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling
 of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:3
+ A1 <<<<
    + CategoryInfo          : ObjectNotFound: (A1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

我一定误解了PowerShell的一些概念。我不能在脚本文件中定义函数吗?

注意,我已经将我的执行策略设置为“remotessigned”。我知道运行。ps1文件时,在文件名前加一个点:.\myFile.ps1

例子如下:

#include <iostream>
using namespace std;

int main()
{
    cout << "Hola, moondo.\n";
}

它抛出错误:

gcc -c main.cpp gcc -o edit main.o  main.o: In function `main':
main.cpp:(.text+0xa): undefined reference to `std::cout'
main.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char,std::char_traits<char> >& std::operator<< <std::char_traits<char>>(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
main.o: In function `__static_initialization_and_destruction_0(int,int)':
main.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::~Init()' collect2: error: ld
returned 1 exit status make: *** [qs] Error 1

还有这个例子:

#include <iostream>

int main()
{
    std::cout << "Hola, moondo.\n";
}

抛出错误:

gcc -c main.cpp gcc -o edit main.o  main.o: In function `main':
main.cpp:(.text+0xa): undefined reference to `std::cout'
main.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char,std::char_traits<char> >& std::operator<<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char> >&, char const*)'
main.o: In function `__static_initialization_and_destruction_0(int,int)': main.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::~Init()' collect2: error: ld
returned 1 exit status make: *** [qs] Error 1

注意:我使用的是Debian 7 (Wheezy)。

是否可以将lambda函数作为函数指针传递?如果是这样,我一定是做了错误的事情,因为我得到了一个编译错误。

考虑下面的例子

using DecisionFn = bool(*)();

class Decide
{
public:
    Decide(DecisionFn dec) : _dec{dec} {}
private:
    DecisionFn _dec;
};

int main()
{
    int x = 5;
    Decide greaterThanThree{ [x](){ return x > 3; } };
    return 0;
}

当我尝试编译这个时,我得到以下编译错误:

In function 'int main()':
17:31: error: the value of 'x' is not usable in a constant expression
16:9:  note: 'int x' is not const
17:53: error: no matching function for call to 'Decide::Decide(<brace-enclosed initializer list>)'
17:53: note: candidates are:
9:5:   note: Decide::Decide(DecisionFn)
9:5:   note: no known conversion for argument 1 from 'main()::<lambda()>' to 'DecisionFn {aka bool (*)()}'
6:7:   note: constexpr Decide::Decide(const Decide&)
6:7:   note: no known conversion for argument 1 from 'main()::<lambda()>' to 'const Decide&'
6:7:   note: constexpr Decide::Decide(Decide&&)
6:7:   note: no known conversion for argument 1 from 'main()::<lambda()>' to 'Decide&&'

这是一个要消化的错误消息,但我认为我从中得到的是不能作为constexpr处理,因此我不能将它作为函数指针传递?我也试着让x成为constexpr,但这似乎没有帮助。

短的例子:

#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关键字?它与传统的参数传递到命名函数有很大不同。背后的原理是什么?

在我的印象中,按值捕获的全部意义在于允许用户更改临时对象——否则我几乎总是使用按引用捕获更好,不是吗?

有什么启示吗?

(顺便说一下,我用的是MSVC2010。这应该是标准的)

在Stack Overflow问题中,c++ 11中不允许重新定义lambda,为什么?,给出了一个不能编译的小程序:

int main() {
    auto test = []{};
    test = []{};
}

问题得到了回答,一切似乎都很好。然后约翰内斯·绍布做了一个有趣的观察:

如果你在第一个前面加一个+,它就神奇地开始工作了。

所以我很好奇:为什么下面的方法有效?

int main() {
    auto test = +[]{}; // Note the unary operator + before the lambda
    test = []{};
}

它可以在GCC 4.7+和Clang 3.2+下编译。代码符合标准吗?