willem
willem

Reputation: 2717

Visual studio 2019 c++ support of concepts - compiles successfully with error: Why?

I have installed the latest version of visual studio to test concepts. I try e.g.:

struct One{};
struct Two{
    std::string ToString() const
    {
        return "BAM!";
    }
};

template<typename T>
concept hasToString = requires(T t) { t.ToString(); };

template <class T>
void DoString(T& t)
{
    if constexpr (hasToString<T>)
    {
        std::cout << t.ToString() << std::endl;
    }
    else
    {
       std::cout << "not available" << std::endl;
    }
}

int main(int argc, char** argv)
{
    One one{};
    Two two{};
    DoString(one);
    DoString(two);
    return 0;
}

This compiles (with /std::c++latest), and gives the output I expected:

not available
BAM!

However, visual studio community c++ 16.5.0 gives 1 error (even though it completes compilation):

identifier "concept" is undefined

I have no clue why? According to below post, concepts should be supported.

https://devblogs.microsoft.com/cppblog/c20-concepts-are-here-in-visual-studio-2019-version-16-3/

So am I doing something wrong? What? Or is this bug, and if so, is there a way to suppress the error until MS fixes the bug?

Upvotes: 2

Views: 806

Answers (1)

Asteroids With Wings
Asteroids With Wings

Reputation: 17464

You are describing an error coming from Intellisense, the engine that does red squiggly lines in the code editor as you type (and populates a "live" error list as you develop).

Though this is compiling your code, it's actually using a different engine from the one that's actually building your project and producing an executable.

According to the feature announcement you linked to, it's not quite up-to-date yet (not outrageous for a brand new feature):

IntelliSense support is not currently available

Ignore.

Upvotes: 2

Related Questions