std::popcount identifier not found in MSVC 16.6.0

1.6k views Asked by At

Am I doing something wrong or is Microsoft's support of std::popcount broken in version 16.6.0 of Visual Studio?

I'm using Microsoft Visual Studio 16.6.0, with C++ Language Standard set to Preview - Features from the Latest C++ Working Draft (std:c++latest) and trying to compile the popcount sample code from cppreference:

#include <bit>
#include <bitset>
#include <cstdint>
#include <initializer_list>
#include <iostream>

int main()
{
    for (std::uint8_t i : { 0, 0b11111111, 0b00011101 }) {
        std::cout << "popcount(0b" << std::bitset<8>(i) << ") = "
            << std::popcount(i) << '\n';
    }
}

Even though cppreference states that bit operations (P0553R4) have been supported since version 16.5 and MSVC 16.6 defines the feature macro __cpp_lib_bitops, the above code gives the following errors:

Error   C3861   'popcount': identifier not found    ConsoleApplication3 C:\Users\rsjaf\source\repos\ConsoleApplication3\ConsoleApplication3\ConsoleApplication3.cpp 22  
Message     see declaration of 'std'    ConsoleApplication3 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\iostream    19  
Error (active)  E0135   namespace "std" has no member "popcount"    ConsoleApplication3 C:\Users\rsjaf\source\repos\ConsoleApplication3\ConsoleApplication3\ConsoleApplication3.cpp 22  
Error   C2039   'popcount': is not a member of 'std'    ConsoleApplication3 C:\Users\rsjaf\source\repos\ConsoleApplication3\ConsoleApplication3\ConsoleApplication3.cpp 22  

When I look in the bit header, I do see a template for popcount, but it seems to be disabled for my application.

1

There are 1 answers

0
Alex Guteniev On

With current MSVC std::popcount is available under /std:c++20 and /std:c++latest.


As @chris pointed out, the feature was not implemented yet, since runtime CPU feature detection is not yet implemented.

The PR to finalize and enable it is in work in progress state: https://github.com/microsoft/STL/pull/795

Defining __cpp_lib_bitops (for intellisense) and __cpp_lib_int_pow2 (generally) before implementing them was a bug. It was fixed by https://github.com/microsoft/STL/pull/695 , but the fix may be still not available for latest version due to the latency of changes integration.