简体   繁体   中英

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). 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)?

#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)?"

#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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM