简体   繁体   English

奇怪的重复模板模式(CRTP)和派生的构造函数参数

[英]Curiously recurring template pattern (CRTP) and derived constructor arguments

I'm using curiously recurring template pattern for creating shared pointer in the following way (below). 我使用奇怪的重复模板模式通过以下方式创建共享指针(如下)。 On Derived::create(...) Visual Studio IntelliSense shows than available arguments are (Args &&...args). 在Derived :: create(...)上,Visual Studio IntelliSense显示的可用参数是(Args && ... args)。 How to pass Derived class constructor argument list to Base so that IntelliSense would show me that available arguments are (const std::string &str, int i)? 如何将派生类的构造函数参数列表传递给Base,以便IntelliSense告诉我可用的参数是(const std :: string&str,int i)?

#include <memory>
#include <string>

template<typename T>
class Base
{
public:
    template<typename... Args >
    static std::shared_ptr<T> create(Args&&... args)
    {
        return std::make_shared<T>(std::forward<Args>(args)...);
    }
};

class Derived : public Base<Derived>
{
public:
    Derived(const std::string &str, int i) {}
};

int main()
{
    auto derived = Derived::create("text", 123);
}

"How to pass Derived class constructor argument list to Base so that IntelliSense would show me that available arguments are (const std::string &str, int i)?" “如何将Derived类构造函数参数列表传递给Base,以便IntelliSense告诉我可用的参数是(const std :: string&str,int i)?”

#include <string>
#include <memory>

template<typename T>
class Base {
public:
    template<typename... Args >
    static std::shared_ptr<T> create(Args&&... args) {
        return std::make_shared<T>(std::forward<Args>(args)...);
    }
};

class Derived : public Base<Derived> {
public:
    Derived(const std::string &str, int i) {}
};

int main() {
    auto derived = Derived::create("text", 123);
}

Well, your code just compiles fine . 好吧,您的代码可以正常编译

Intellisense features of any IDE are always just as good as the c++ parser used for them. 任何IDE的Intellisense功能始终与用于它们的c ++解析器一样好。 That's completly dependent on the IDE actually used, and you shouldn't orient your designs about your IDE's capabilities, but what compiles and works well. 这完全取决于实际使用的IDE,并且您不应该针对IDE的功能来设计自己的设计,而是可以编译并很好地进行工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CRTP(Curious Recurring Template Pattern)中的模板派生类 - Templated derived class in CRTP (Curiously Recurring Template Pattern) 为什么奇怪的重复模板模式(CRTP)的工作原理 - why Curiously Recurring Template Pattern (CRTP) works 什么是奇怪的重复模板模式(CRTP)? - What is the curiously recurring template pattern (CRTP)? CRTP(奇怪的重复模板模式)使用通用基础模板类而不是派生类 - CRTP (Curiously Recurring Template Pattern) using a generic base template class instead of the derived class 使用Clang中的静态constexpr奇怪地重现模板模式(CRTP) - Curiously recurring template pattern (CRTP) with static constexpr in Clang 奇怪的重复模板模式(CRTP),AutoLists和C ++ - Curiously Recurring Template Pattern (CRTP), AutoLists and C++ 好奇的重复模板模式(CRTP)是否是正确的解决方案? - Is the Curiously Recurring Template Pattern (CRTP) the right solution here? 使用奇怪的重复模板模式(CRTP)和其他类型参数 - Use Curiously Recurring Template Pattern (CRTP) with additional type parameters 以奇怪的重复模板模式访问更多派生类函数/值 - Access More Derived Class functions/values in Curiously Recurring Template pattern 单身人士,好奇地反复出现模板模式和转发构造函数参数 - Singletons, curiously recurring template pattern and forwarding constructor parameters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM