0

I've been trying to figure out how to properly instantiate a vector of std::pairs of std::functions (e.g. std::vector<std::pair<std::function<bool()>, std::function<void(int)>>>). Upon doing so however, Visual Studios 15 gives me a red underlining warning in my code even though it still compiles. I decided to start with just a basic std::function initialization using std::bind, but that too gave me an underlining error that compiles.

I kind of understand it's that std::bind doesn't return an std::function, but I've seen numerous examples of instantiating std::functions the way I do with the second underlining/warning.

Am I setting myself up for failure here, or is visual studios just giving me a false positive on these errors?

Error that compiles 1

Error that compiles 2

CODE:

#include <functional>
#include <vector>


class A {
public:
    bool myFunc() {
        return false;
    };
    void myDoFunc(int x) {
        std::cout << "HiXXX";
    };
    class Seq{
    public:
        std::vector<std::pair<std::function<bool()>, std::function<void(int)>>> sequencePieces;
        const char* name;

        Seq(const char* _name, std::vector<std::pair<std::function<bool()>, std::function<void(int)>>> _sequencePieces):
            name(_name),
            sequencePieces(_sequencePieces)
        {}
    };
    std::function<bool()> sequenceStep;
    Seq seq1;

    A() :
        seq1( "ASDF",
    {
        /*Error 1*/
        std::pair<std::function<bool()>, std::function<void(int)>>(
            std::bind(&A::myFunc,this),
            std::bind(&A::myDoFunc, this, 1)
            )
    }
        )
    {
        /*Error 2*/
        sequenceStep = std::bind(&A::myFunc, *this);
    };
};
  • 1
    Fired that up and duplicated. That's Iintellisence barfing. Think of Intellisense as a compiler that's allowed to be wrong in order for it to be fast enough to provide near-realtime feedback. In this case it's missing something. – user4581301 Jun 06 '22 at 18:23
  • If you upgrade to [VS2022](https://visualstudio.microsoft.com/vs/community/), does it still have this problem? – Eljay Jun 06 '22 at 18:30
  • @Eljay Circumstances restrict me to VS15 (Update 3) and C++ 11 – StephBoyardee Jun 06 '22 at 18:58
  • Interesting conundrum you are facing, since VS15 doesn't support C++11. It does support C++14 — hopefully that's sufficiently adequate for your use case. It also supports C11 (**C**, not **C++**). I think user4581301 nailed the problem: IntelliSense's parser was built for incredible performance rather than perfect accuracy, so there are some discrepancies. – Eljay Jun 06 '22 at 19:08

0 Answers0