【问题标题】:How can variadic template represent specific struct constructor`s parameters in IntelliSense可变参数模板如何在 IntelliSense 中表示特定的结构构造函数参数
【发布时间】:2022-01-21 10:33:52
【问题描述】:

我希望看到有关 make_unique 等构造函数的智能感知。我查看了 make_unique 并尝试了以下操作(我可能是错的),但没有成功。

以下代码显示了我想要做什么,而不是问题。

#include <memory>

struct Struct
{
    Struct(int a)
    {

    }
};

template <typename Type, typename... Args>
Type* make_struct(Args&&... args)
{
    return new Type(std::forward<Type>(args)...);
}

int main()
{
    // std::make_unique can see the constructor in intelliSense.
    std::unique_ptr<Struct> a = std::make_unique<Struct>(3); 

    // make_struct can`t see the constructor in intelliSense.
    std::unique_ptr<Struct> c(make_struct<Struct>(3));
}

【问题讨论】:

  • make_struct 有什么问题?除了不使用转发引用是
  • 您可能需要从一个更简单的练习开始。发生了几件事情,不清楚从哪里开始解释。
  • I want the variadic template to appear as constructor parameters 你想写template&lt;typename A, template &lt;typename... Args&gt; class B&gt; Struct (B&lt;A&gt; arg);?这里B 是一个可变参数模板,出现在Struct 的构造函数中。你能解释一下你为什么要制作make_unique2,为什么它在std里面,和你的问题有什么关系?
  • "//make_struct在intelliSense中看不到构造函数。"什么意思?此行之后的行编译得很好,并且在 VS 中没有显示任何问题。顺便说一句,您在谈论哪个构造函数? std::unique_ptr&lt;Struct&gt; 的自动补全中显示的构造函数 3/7 是 std::unique_ptr&lt;Struct&gt;::unique_ptr(Struct*)

标签: c++ visual-studio-2019 intellisense variadic-templates


【解决方案1】:

您正在调用 make_unique&lt;Struct&gt;(...(即将 Struct 作为模板参数传递)但 make_struct 没有任何模板参数。第二个无法推断出您要制作的对象的类型。

要么删除额外的模板参数,即

template <typename... Args>
Struct* make_struct(Args&&... args)
{
    return new Struct(std::forward<Args>(args)...);
}

或将“不可演绎”参数传递给调用,即

make_struct<Struct>(...); // Doesn't really make sense since the very name
                          // Implies you're making a struct. 

【讨论】:

  • 对不起。还是不行。
  • @김범무 我看到你编辑了标题以包含“intellisence”
  • 我还是不习惯问好问题。对不起。
  • @김범무 这意味着“内部”代码是以最好的方式编写的,以促进提供智能感知的模糊解析器,你可能不会这样做。例如。甚至您编写 cmets 的方式也会影响智能感知显示的内容、包含顺序、宏扩展等。
  • @김범무 在这里查看如何best configure a C++ project for intellisense。请注意,VS 使用专用编译器来执行 IntelliSense,这意味着编译器最终执行的操作与 IntelliSense 通知您的内容之间可能存在不匹配。
猜你喜欢
  • 2018-05-18
  • 2016-09-02
  • 2016-01-02
  • 2015-05-06
  • 2014-04-21
  • 1970-01-01
  • 1970-01-01
  • 2019-01-29
  • 2014-11-09
相关资源
最近更新 更多