我不断听说该inline关键字不再用作现代编译器的提示,而是用于避免多源项目中的多重定义错误。
但今天我遇到了一个编译器遵守关键字的例子。
没有inline关键字的话,代码如下
#include <iostream>
using namespace std;
void func(const int x){
if(x > 3)
cout << "HAHA\n";
else
cout << "KKK\n";
}
int main(){
func(5);
}
Run Code Online (Sandbox Code Playgroud)
使用命令g++ -O3 -S a.cpp,生成未内联的汇编代码func。
但是,如果我在 的定义前面添加 inline 关键字func,则func会内联到 中main。
生成的汇编代码部分是
.LC0:
.string "HAHA\n"
.LC1:
.string "KKK\n"
.text
.p2align 4,,15
.globl _Z4funci
.type _Z4funci, @function
_Z4funci:
.LFB975:
.cfi_startproc
cmpl $3, %edi
jg .L6
movl $4, %edx
movl $.LC1, %esi
movl …Run Code Online (Sandbox Code Playgroud) 码:
class A {
std::vector<int> x = {2,3}; // x[0] = 2 and x[1] = 3
std::vector<int> y = std::vector<int>(2,3); // x[0] = 3 and x[1] = 3 Too verbose!!
};
Run Code Online (Sandbox Code Playgroud)
有没有办法可以调用std::vector<int>只使用大括号初始化程序的构造函数,或者至少是更短的版本,它会产生相同的效果?
我不想重复std::vector<int>.
这是一个简单而古老的问题。我相信它必须在其他地方问。但经过 30 多分钟的谷歌搜索后,我决定再次在这里提问。关于右值引用的大量信息让我很难找到适合这个问题的网页。
右值不能绑定左值引用的这个决定背后的原因是什么?
最近,我发现 Boost.Heap 在我的项目中非常有用。但我找不到任何示例代码来显示如何设置任意比较函数。
#include "boost/heap/fibonacci_heap.hpp"
using boost::heap::fibonacci_heap;
int main(){
fibonacci_heap<int> pq; //default compare function std::less<int>
}
Run Code Online (Sandbox Code Playgroud)
例如,如何设置 std::greater< int >?
boost.heap文档说可以通过设置一个选项来设置。但我不知道这意味着什么。有人可以帮忙吗?
我很难使用variadic模板与以下问题.
假设所有谓词仿函数都是这种形式,
class Pred1 {
public:
Pred1( Args... ); // The signature Args... can vary class to class.
template <typename T>
bool operator()(T t);
};
Run Code Online (Sandbox Code Playgroud)
给定这些仿函数,我想创建一个可变参数模板类,如果每个谓词的所有operator()都返回true,则返回true,即
template <typename... Preds>
class CombinePredAnd {
public:
template <typename T>
bool operator()(T t){
// returns true if all of the Preds( Args... ).operator()(t) returns true;
// Args... should be passed when CombinePredAnd is constructed.
}
};
Run Code Online (Sandbox Code Playgroud)
对我来说,我不知道将参数传递给Preds的每个构造函数.你能给我一些提示吗?此外,如果您有更好的设计具有相同的功能,请告诉我.