1

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);
}
3
  • 3
    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). Commented Feb 7, 2015 at 21:11
  • @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." Commented Feb 7, 2015 at 21:17
  • 2
    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. Commented Feb 7, 2015 at 21:35

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

4 Comments

He didn't ask how to make the code compile (it was already fine), he asked how to modify the code to play nice with IntelliSense. This in no way answers that question.
@ildjarn I well stated that the code compiles. Visual Studio doesn't use thes same parser for the c++ code as it's used for the compiler. There are a number of cases where intellisense may not work well AFAIK. Hence my statement to rely on the compiler rather than intellisense.
Which still doesn't answer the actual question.
@Zulis Then your solution would be to create overloads for the constructor, as proposed in the 1st comment on your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.