【发布时间】:2017-03-26 22:07:29
【问题描述】:
我正在探索将 friend 关键字与可变参数模板、完美转发和私有构造函数一起使用。我觉得我错过了所有这一切的基本内容,因为下面的简单示例无法编译。
我希望Test_Manager<Test_Class>::Process 成为构造Test_Class 类型对象的唯一方法(Process 最终会做得更多,但这是一个微不足道的例子)。我还希望Test_Manager 能够以这种方式“管理”各种类,因此类类型的参数t_Symbol 和可变参数...t_Args 来处理各种构造函数。
// Test.cpp
#include <string>
template<typename t_Symbol>
struct Test_Manager
{
template<typename... t_Args>
static t_Symbol Process(const t_Args&... i_Args)
{
const t_Symbol New_Symbol(std::forward<t_Args>(i_Args)...); // error C2665
return New_Symbol;
}
};
class Test_Class
{
private:
friend Test_Manager<Test_Class>;
Test_Class() {};
Test_Class(const std::string& i_Text) : m_Text(i_Text) {};
const std::string m_Text;
};
void Test_Function()
{
std::string text = "hello_world";
Test_Class t = Test_Manager<Test_Class>::Process(text);
}
但是,在 Visual Studio 2015 Update 3 中,我收到以下错误(在上面标记的行):error C2665: 'std::forward': none of the 2 overloads could convert all the argument types。我在这里搞砸什么?我觉得这应该可行。
【问题讨论】:
标签: c++ variadic-templates perfect-forwarding