在C++中,似乎参数通常可以...直接在参数包名称之后扩展。例如,
template <class... Tys>
void function(Tys... params) {
function(params...);
}
Run Code Online (Sandbox Code Playgroud)
然而,当使用 时std::forward,...看起来似乎在参数包名称后面。例如,
template <class... Tys>
void function(Tys... params) {
function(std::forward<Tys>(params)...); // The '...' is outside the parenthesis of 'params'
}
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么会有差异?为什么下面的代码不正确?
template<class... Tys>
void function(Tys... params) {
function(std::forward<Tys>(params...));
}
Run Code Online (Sandbox Code Playgroud)
参数包在什么时候展开?我可能不完全理解它是如何std::forward工作的或者它如何完美地转发论点。
我很感激任何答案!
c++ templates variadic-functions variadic-templates perfect-forwarding
回顾微软的STL的代码(具体来说std::vector),我发现了以下几行代码(无关代码替换为/* ... */):
// CLASS TEMPLATE vector
template <class _Ty, class _Alloc = allocator<_Ty>>
class vector // varying size array of values
{
/* ... */
public:
/* ... */
using value_type = _Ty;
using allocator_type = _Alloc;
using pointer = typename _Alty_traits::pointer;
using const_pointer = typename _Alty_traits::const_pointer;
using reference = _Ty&;
using const_reference = const _Ty&;
using size_type = typename _Alty_traits::size_type;
using difference_type = typename _Alty_traits::difference_type;
/* ... */
};
Run Code Online (Sandbox Code Playgroud)
我想知道为什么这里使用为模板类型分配类型别名的约定?
在摆弄类型双关迭代器时,我发现了这样做的能力
std::vector<int> vec{ 3, 7, 1, 8, 4 };
int* begin_i = (int*)(void*)&*vec.begin();
std::cout << "1st: " << begin_i << " = " << *begin_i << std::endl;
begin_i++;
std::cout << "2nd: " << begin_i << " = " << *begin_i << std::endl;
Run Code Online (Sandbox Code Playgroud)
然后我尝试用以下方法做同样的事情std::unordered_set:
std::unordered_set<int> set{ 3, 7, 1, 8, 4 };
for (auto& el : set)
{ // Display the order the set is currently in
std::cout << el << ", ";
}
std::cout << '\n' <<std::endl;
int* …Run Code Online (Sandbox Code Playgroud) TLDR - 如果是这样,请随意将其标记为重复;我会删除问题。然而,我环顾四周后没有找到任何东西。
考虑以下类:
class Base
{
public:
Base(std::string var1);
Base& operator=(Base&& other) noexcept
{
if (this != &other)
var1_ = std::move(other.var1_);
return *this;
}
protected:
std::string var1_;
};
class Child final : public Base
{
public:
Child(std::string var1, std::string var2);
Child& operator=(Child&& other) noexcept
{
if (this != &other)
{
// Take note of HERE for explanation below
Base::operator=(std::move(other));
var2_ = std::move(other.var2_);
}
}
private:
std::string var2_;
};
Run Code Online (Sandbox Code Playgroud)
这里有两个类,Child从Base. Child拥有比 多的成员Base。在 的移动赋值运算符中 …
我是一名使用 C++ 工作的新程序员,我正在尝试编写一个程序,将信息从文件导入到输出文件,然后我将对数据执行搜索算法。我正在尝试使用数据结构并将其导入到数组中,然后在主程序中调用它。
由于某种原因,我一生都无法让我的函数调用正常工作;inputFile我在主程序的函数调用中不断收到未声明的标识符错误。我意识到我可能正在做一些根本性错误的事情,所以我非常感谢可以提供的任何帮助。
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <fstream>
using namespace std;
const int MAX_LOG_SIZE = 7584;
const string LOGFILE ="crimes.dat";
const string OUTPUT_FILE ="crimesorted.log";
// Structure of strings based on info from crimes.dat
struct CrimeInfo
{
string Crimedescr;
string Date;
string Time;
string Address;
string Grid;
string Latitude;
string Longitude;
};
CrimeInfo crimeList [MAX_LOG_SIZE];
void openInputFile(ifstream& inputFile, string inputFilename)
// here we open the input file crimes.dat
{
inputFile.open(inputFilename.c_str());
while (inputFile.fail())
{ …Run Code Online (Sandbox Code Playgroud) c++ ×5
memory ×2
templates ×2
class ×1
hash ×1
inheritance ×1
iostream ×1
pointers ×1
std ×1
type-alias ×1
type-punning ×1