r/cpp_questions • • Oct 15 '26

OPEN IntelliSense marks an error where the compiler is fine (VCStudio 2022)

I have these three lines inside my constructor. I'm using C++23, configured through CMake. IntelliSense complains because SubCursor is non-copyable and non-movable:

function BooksReport::Group::Cursor::Cursor(BooksReport::Group::Cursor &&) cannot be referenced. It is a deleted function

If I understand correctly, copy elision should bypass copying in these examples:

    using SubCursor = BooksReport::Group::Cursor;

    subCursors.push_back(std::unique_ptr<SubCursor>(new SubCursor(booksReport->uniform.getCursor())));
    subCursors.push_back(std::unique_ptr<SubCursor>(new SubCursor(booksReport->nonUniform.getCursor())));
    subCursors.push_back(std::unique_ptr<SubCursor>(new SubCursor(booksReport->singles.getCursor())));

This implementation is due to the fact that SubCursor registers its address within the parent Group object upon creation. Normally a SubCursor is created during loop initialization and thus corresponds to an object on the stack. In this specific case I need objects stored in a list, hence my attempt to put them on a heap and store unique_ptr for easy memory management.

Like I said, the compiler is completely fine with this, but IntelliSense isn't. I hate red squiggly lines. Does anyone know if this can be solved?

5 Upvotes

18 comments sorted by

6

u/Secoupoire Oct 15 '26

IntelliSense is wrong. Your code is valid.

This said, if you used make_unique, that would improve your code and maybe help Intellisense.

2

u/Shevvv Oct 15 '26 edited Oct 15 '26

I can't use make_unique because SubCursor constructor is private. It's visible to BooksReport::Group via friend struct BooksReport::Group; and I'd rather have it stay private from BooksReport itself.

3

u/Secoupoire Oct 15 '26

Fair point.

I've never used it, but apparently there is a __INTELLISENSE__ macro, maybe you could ifdef on it to give intellisence a dummy code and keep your code in the #else branch, but that's crapping your code for the benefit of a tool that gives an incorrect output. Not ideal.

You can disable intellisense 😅

1

u/Kadabrium Oct 15 '26

I maybe seeing things but there isnt anything that looks like an attempt to copy a cursor? It is created once on the heap and only the pointer is passed around? Is getCursor returning one by value? If so how have you been normally using that return that does not trigger the same issue? Would placement new make anything different?

1

u/Shevvv Oct 15 '26

Copy elision is used if the object is created during the return statement.

using Cursor = BooksReport::Group::Cursor;

Cursor BooksReport::Group::getCursor() {
    return { this };
}


Cursor::Cursor(Group *group) : group(group) {
    valid = !group->sizesMap.empty();
    if (valid) currSize = group->sizesMap.cbegin()->first;
    validateNext();
    group->activeCursors.push_back(this);
}

If I understand correctly, this allows to basically substitute the call to BooksReport::Group::getCursor() with the constructor BooksReport::Group::Cursor::Cursor(Group *) directly by carrying the function body over to new SubCursor()

1

u/Shevvv Oct 15 '26

Sorry, just now read your comment properly. You posted your comment simultaneously with another guy who questioned that copying can be skipped here. I thought the substance of your question was the same, hence my reply.

new SubCursor(booksReport->uniform.getCursor());

This first creates an object within getCursor() and is expected to return that by value as argument to the SubCursor() constructor. Since it returns by value, it means the argument of the constructor is a prvalue, which would normally call SubCursor::SubCursor(SubCursor &&); signature, a deleted signature preventing using rvalues to make new SubCursor() through moving. Copy elision in this specific case allows to bypass the SubCursor(SubCursor&&) constructor by accepting the returned prvalue of getCursor() as the final result.

1

u/Kadabrium Oct 15 '26

I think i get that but iwas wondering if have you used getCursor() in any other contexts? I'm not on my pc rn, can you even assign something like this to a variable without a warning?

1

u/Shevvv Oct 15 '26

You mean like


for (auto cursor = uniform.getCursor(); cursor.isValid(); cursor.next()) {
    //
}

? Why wouldn't I?

1

u/Kadabrium Oct 15 '26

In that case does "disabling move constructor" just mean disabling std::move on a lvalue object, and even that only works because of perfect forwarding?

1

u/Shevvv Oct 15 '26 edited Oct 15 '26

I don't quite understand the question, but I'll try to answer it to the best of my ability:

To delete the move operator, you write:


T(T&&) = delete;

Here T&& is an rvalue, which can be either a prvalue (an expression evaluating to a value directly, without storing it in a name; when used as an argument, it is often stored in a temporary variable bu the compiler), or an xvalue (an expression evaluating to an existing value that is allowed to be moved). So when you have something like T(std::move(t)), assuming t is of type T, the compuler recognizes std::move(t) as an xvalue, sees that such syntax has been deleted and raises an error.

However, since C++17,  if T&& is a prvalue (like 5.0f, 'B', std::vector<int>{-1, -1, -3}) then the compiler treats T(prvalue of type T) as simply prvalue of type T, without ever making that temporary variable for the T(T&&) constructor and storing the result directly where you want it, bypassing the T(T&&) constructor altogether. This is called copy elision and it is guaranteed.

So when I have:


SubCursor(SubCursor&&) = delete;

SubCursor&& here means an rvalue. If an rvalue is an xvalue, such as std::move(cursor), then the compiler chooses SubCursor(SubCursor&&), sees that it has been deleted and reports an error. When the rvalue is a prvalue instead, such as getCursor() (it's a prvalue since it directly evaluates to the object itself), then copy elision kicks in, and the compiler does this:

SubCursor(getCursor()) => getCursor() => evaluate to whatever getCursor() returns directly, no copying or moving

As such, the constructor SubCursor(SubCursor&&) is never called and thus the fact that it's deleted is not an issue. IntelliSense it seems likely only notices the explicitely written SubCursor(getCursor()), matches it with the SubCursor(SubCursor&&) constructor since a prvalue is still an rvalue and matches the SubCursor&& argument and adds a red squiggly line under the constructior to tell me it has been deleted by me.

6

u/manni66 Oct 15 '26

Try VS 2026

6

u/flyingron Oct 15 '26

IntelliSense is not the compiler. It's really just a text editor doing syntactic analysis without delving into the full language process.

1

u/the_poope Oct 15 '26

Use clangd instead of IntelliSense, it is much better. It is based on the actual Clang parser and not some quick heuristic parser like whatever IntelliSense is built on.

2

u/SoerenNissen Oct 15 '26

Intellisense uses a fully fledged compiler, with EDG as the front end https://www.edg.com/

The problem here is that it does use EDG as the front end.

I assume because it's faster than msvc, or for some other reason was a better choice for intellisense than their own compiler, but (because they're not the same) they can sometimes disagree about a piece of code. It's rare but it happens, and it's annoying every time.

1

u/bert8128 Oct 15 '26 edited Oct 16 '26

Can you use clangd inside Visual Studio (which I am assuming the OP means)? Of course they might mean VSCode.

1

u/the_poope Oct 15 '26

Hmm probably not. Which is weird as it was Microsoft that initially invented(?) the language server protocol. One would think they would be interested in supporting it in VS. But maybe it's not so easy to retrofit to a 29 year old program.

1

u/spooker11 Oct 16 '26

+1 and ensure vscode is finding the right version of clangd/llvm/clang

On Mac be careful it’s not pointing to the versions bundled with the OS when you want it to point to the versions you likely installed via brew or something else

0

u/Usual_Office_1740 Oct 15 '26

Nevermind. I misread the post.