'random_shuffle': is not a member of 'std' error

Viewed 12403

I'm trying to use std::random_shuffle, and get a compilation error.

My compiler is v140(Visual Studio 2015), and I work on x64, Release mode.

My code:

#include <random>
#include <algorithm>
void foo()
{
    std::vector<int> v;
    std::random_shuffle(v.begin(), v.end());
}

The errors which I get:

error C2039: 'random_shuffle': is not a member of 'std'
error C3861: 'random_shuffle': identifier not found
  • Any idea what could the problem be?

Thanks!

3 Answers

TLDR:

I was in Visual Studio and faced this error.
To fix this, simply set the /Zc:__cplusplus flag in Properties > C/C++ > Command Line and you are good to go.

Long Explanation:

I faced this issue when trying to use Hayai. In visual studio the __cplusplus is always set as 199711L and Hayai that also checks for this always fails in visual studio becasue of this:

        /// Randomly shuffles the order of tests.
        static void ShuffleTests()
        {
            Benchmarker& instance = Instance();
#if __cplusplus > 201100L
            std::random_device rd;
            std::mt19937 g(rd());
            std::shuffle(instance._tests.begin(),
                         instance._tests.end(),
                         g);
#else
            std::random_shuffle(instance._tests.begin(),
                                instance._tests.end());
#endif
        }

As you can see becasue of this issue in visual studio it always branches into the wrong snippet and thus fail.
To fix this, simply set the /Zc:__cplusplus flag in Properties > C/C++ > Command Line and you are good to go.

From Microsoft:

The /Zc:__cplusplus compiler option enables the __cplusplus preprocessor macro to report an updated value for recent C++ language standards support. By default, Visual Studio always returns the value "199711L" for the __cplusplus preprocessor macro.

Remarks


The __cplusplus preprocessor macro is commonly used to report support for a particular version of the C++ standard. Because lots of existing code appears to depend on the value of this macro matching "199711L", the compiler does not change the value of the macro unless you explicitly opt-in by using the /Zc:__cplusplus compiler option. The /Zc:__cplusplus option is available starting in Visual Studio 2017 version 15.7, and is off by default. In earlier versions of Visual Studio, and by default, or if /Zc:__cplusplus- is specified, Visual Studio returns the value "199711L" for the __cplusplus preprocessor macro. The /permissive- option does not enable /Zc:__cplusplus.

When the /Zc:__cplusplus option is enabled, the value reported by the __cplusplus macro depends on the /std version switch setting. This table shows the possible values for the macro:

Make sure to read the actual article Here

Related