C++ says gcd is not a member of std - c++

I am using visual studio 2017 and I have run across a problem. When trying to use std::gcd it gives me an error error C2039: 'gcd': is not a member of 'std'
Here's my code:
#include "pch.h"
#include <iostream>
#include <numeric>
int main() {
std::cout << std::gcd(10, 5);
return 1;
}

std::gcd was added in C++17. To use it in Visual Studio you need to specify the language standard. You can do that two ways, use the /std:c++17 command-line option or in the Project Properties dialog: C/C++ -> Language -> C++ Language Standard.

I just tested and got the same error with VS2017 15.8.9 after having set the language standard to C++17. When I checked my project settings again, the language setting I made had reverted back to the default. After having set it a second time, it worked.
This seems to happen often when I start a new project and change to C++17 directly.

Related

WinRT GetSystemIdForPublisher() unable to execute due to C++20 coroutines?

I am trying to get the system ID using
auto info = winrt::Windows::System::Profile::SystemIdentification::GetSystemIdForPublisher();
auto id = info.Id();
auto asHex = winrt::Windows::Security::Cryptography::CryptographicBuffer::EncodeToHexString(id);
But when I try to run this code I get errors E0035 and C1189 and both have the exact same description:
The <experimental/coroutine> and <experimental/resumable> headers are only supported with /await and implement pre-C++20 coroutine support. Use coroutine for standard C++20 coroutines.
(Note: the italic coroutine above is inside <>, as soon as I put <> around the a word it disappears so I have omitted it)
I am including all the relevant files in my header that I think might be needed:
#include <Windows.h>
#include <coroutine>
#include <windows.foundation.h>
#include <winrt/Windows.System.Profile.h>
#include <winrt/Windows.Security.Cryptography.h>
I am using:
Microsoft Visual Studio Community 2019
Microsoft Visual C++ 2019
Windows 10 SDK 10.0.19041.0
In your project, if you go to
Property Pages ->
Configuration Properties ->
General ->
C++ Language Standard,
do you by any chance have it set to "ISO C++20 Standard (/std:c++20)"?
C++/WinRT only runs C++17.
A few hours ago I realized that C++/WinRT exists, and I am currently trying understand the fundamentals of it. If my memory serves me correctly, I also got this exact error message when trying some not-at-all-copy-pasted-from-msdn code out. Although I am using Visual Studio 2022 (Community Edition), I do not see why that would make a difference.
I hope this is of any use.
Kind regards,
a not so experienced programmer

'optional': is not a member of 'std' in Visual Studio

The code I am writing is throwing this error:
'optional': is not a member of 'std'
I understand that the file <optional> is located in MSVC/tools and this external dependencies section is usually populated by Intellisense, but the file appears not to be included even though I confirmed that the file does indeed exist and I have #include <optional>.
What is the best way to close the gap here?
Is there a way to tell Visual Studio to include all the MSVC tools?
Code snippet from header:
#pragma once
#include Examples.h
#include <optional>
#include <vector>
namespace Samples
Code snippet from cpp:
#include 'Examples.h'
std::optional<Samples::Matrix> Samples::TestFunction()
You must have your C++ Language Standard option in Project Settings set to C++17 or later:
Right-Click on the project in the Solution Explorer
Select Properties
Under Configuration Properties > General > C++ Language Standard
Select ISO C++17 Standard (/std:c++17) or Preview Latest (/std:latest)
Future readers: ISO C++20 Standard (/std:c++20) is also an option.
Click OK
Save All to save the changes to the project.

Why does checked_array_iterator work without including <iterator> in VS2013 but fails in VS2017?

Given the following code:
#include <memory>
#include <locale>
int main() {
auto source = std::make_unique<int[]>(16);
auto dest = std::make_unique<int[]>(16);
auto dp = stdext::checked_array_iterator<int*>(dest.get(), 16);
std::copy_n(source.get(), 16, dp);
return 0;
}
It compiles cleanly on Visual Studio 2013 by running cl.exe /EHsc <file>.cpp. However, on Visual Studio 2017 the following errors (among others) are thrown by cl.exe:
vc12.cpp(7): error C2653: 'stdext': is not a class or namespace name
vc12.cpp(7): error C2065: 'checked_array_iterator': undeclared identifier
Why does this code no longer compile?
The example is missing an #include <iterator>. Technically, it was also missing for Visual Studio 2013 (see Why include what you use), but due to the chain of includes it worked there. Between Visual Studio 2013 and Visual Studio 2017 the includes of the std-headers got revamped.
The example shows #include <locale>. In the old version, #include <iterator> was part of the include-chain of locale, which is no longer the case in Visual Studio 2017.
However, the documentation of VS2017 is hard to find at the moment since it is so new. The important document can be found at doc.microsoft.com, it lists the required header <iterator>.
This is included in visual-studio-2017 according to the documentation: https://learn.microsoft.com/en-us/cpp/standard-library/checked-array-iterator-class However you'll need to #include <iterator>.
Though rather than pursuing correcting this, I'd entourage writing standard conformant code. The best way to do that would be to just use a statically allocated array. This will allow you to use C++'s begin, end, and size functions to work with it: https://stackoverflow.com/a/33442842/2642059
There are some cases where that wouldn't be a good suggestion, if a dynamic array is a must have, consider using a vector. If you can't abide a container then using a unique_ptr is a good way to do this, but rather than depending upon checked_array_iterator prefer maintaining your own size:
const size_t sourceSize = 16;
auto source = std::make_unique<int[]>(sourceSize);
auto dest = std::make_unique<int[]>(sourceSize);
std::copy_n(source.get(), sourceSize, dest.get())

Why might the “fatal error C1001” error occur intermittently when using openmp?

My code works well without #openmp
but I got this error when I added #openmp compiler
1>c:\users\hdd amd ali\documents\v studio 10 projects\visual studio 2010\projects\escaledesvols2 - copy\escaledesvols2\djikstra.cpp(116): fatal error C1001: An internal error occurred in the compiler.
1> (compiler file 'f:\dd\vctools\compiler\utc\src\p2\wvm\mdmiscw.c', ligne 1098)
note:
i use many different libraries (like #boost)
#include <string>
#include <iostream>
#include <stdio.h>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <msclr\marshal_cppstd.h> // for unmanaged piece of code
#include <vcclr.h>
I had this issue recently; I was compiling with visual studio 2015. I tried it with visual studio 2017 and I still got the internal compiler error. Then I tried it with visual studio 2013 and it told me that I can't have a "return" statement inside an openMP section. when I removed the return from VS 2013 and VS 2105 the compiler was able to successfully compile. So, it make sense to try it with VS 2013 and it will give you a better error description. You could also be having return statements inside openMP sections and that could be the reason for c1001 error.
In my case it was a return function from the OpenMP loop. Removing a "return" line solved the problem.
You should simply report it.
In terms of workarounds, it is likely related to memory/resource consumption. Usual tricks to lower consumption are
disable debug information
split up compilation units to smaller size (this might be key here: "I'm using many libraries" should not be an issue unless you're including all the headers in a single translation unit
try to reduce template instantiations
Alternatively
reduce system load (close other programs, such as your Stackoverflow browser that might tatke valuable resources :))

C++ Visual Studio warn about indirect includes

The following code compiles fine with Visual Studio 2013 (probably because <iostream> includes <limits>), but the "missing" #include <limits> prevents me as C++ newbie sometimes to understand whats going on. For example I realized that std::numeric_limits<int>::max() is in <limits> only after removing #include <iostream>.
So how can I force the compiler to require each include explicit?
#include <iostream>
int main() {
std::cout << std::numeric_limits<int>::max();
}
While this isn't a compiler "Warning" per say, you can have the MSVC compiler output a list of all included files at compile time with the /showIncludes flag MSDN compiler reference