gpt4 book ai didi

c++ - 具有折叠表达式的模板会创建不需要的参数拷贝

转载 作者:行者123 更新时间:2023-12-02 02:05:50 25 4
gpt4 key购买 nike

以下原型(prototype)旨在进行同步打印:

#include <iostream>
#include <string>
#include <sstream>
#include <mutex>
#include <Windows.h> // for OutputDebugString

std::mutex sync_mutex;

template<typename T>
void sync_print_impl(std::ostringstream& str, const T& t)
{
str << t << " ";
}

template<typename ... Args>
void sync_print_impl(std::ostringstream& str, Args ... args)
{
str; // prevents unused variable warning when sync_print is called without arguments
(..., sync_print_impl(str, args));
}

template <typename... Args>
void sync_print(Args... args)
{
std::ostringstream s;
sync_print_impl(s, args...);

{
std::lock_guard<std::mutex> lg(sync_mutex);
std::cout << s.str() << std::endl;
}
}

简单测试一下就可以了:

void test1()
{
sync_print("abc", 1, 5.5); // prints abc 1 5.5
sync_print(); // prints empty string
}

以下测试显示参数已被复制:

class test_class
{
public:
test_class(int n)
: data(n)
{
OutputDebugString(L"test_class\n");
}

test_class(const test_class& other)
{
data = other.data;
OutputDebugString(L"copy constructor\n");
}

test_class& operator=(const test_class& other)
{
data = other.data;
OutputDebugString(L"operator=\n");
return *this;
}

~test_class()
{
OutputDebugString(L"~test_class\n");
}

friend std::ostream& operator<<(std::ostream& os, const test_class& t);

private:
int data{};
};

std::ostream& operator<<(std::ostream& os, const test_class& t)
{
os << t.data;
return os;
}

void test2()
{
test_class t(5);
sync_print(t); // prints 5
}

OutputDebugString 结果是:

test_classcopy constructor~test_class~test_class

如何更改sync_print以确保参数通过引用传递?

最佳答案

您不需要额外的模板函数sync_print_impl。以下应该足够了,因为您可以使用 fold expression .

此外,使用perfect forwarding以避免应对该物体。

template<typename... Args>
void sync_print_impl(std::ostringstream& str, Args&&... args)
// ^^^^^^^^^^^^^^^^
{
((str << std::forward<Args>(args) << " "), ...);
}

template <typename... Args>
void sync_print(Args&&... args)
// ^^^^^^^^^^^^^^^
{
std::ostringstream s;
sync_print_impl(s, std::forward<Args>(args)...);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^
// ... code
}

关于c++ - 具有折叠表达式的模板会创建不需要的参数拷贝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68465830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com