1
votes

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);
}
1
You would have to remove the forwarding version and create overloads for each constructor. Intellisense sees the function interface for what it is, how is it supposed to know that you forward to the constructor (it would additionally have to go through make_shared to figure that out). - jepio
@jepio I seriously disagree! That's exactly, what I mean one shouldn't do: "and you shouldn't orient your designs about your IDE's capabilities, but what compiles and works well." - πάντα ῥεῖ
I'm not saying he should do it. I'm telling him what he would have to do and that it is impossible/unreasonable to expect it to work. - jepio

1 Answers

0
votes

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