namespace std:: does not contain optional - c++

i am doing the Vulkan Tutorial
https://vulkan-tutorial.com/
#define GLFW_INCLUE_VULKAN
#include<GLFW/glfw3.h>
#include<optional>
struct s {
std::optional<uint32_t> num;//Intellisense Error
};
int main() {
return 5;
}
I started with an empty project and added includes and libraries;
I can compile and run without including std::optional.
When i use std::optional I get c2039 "optional is not a member of std"
I am running Windows 10, and VisualStudio 2019
What is going on here ?
thx.

std::optional requires C++17.
Live on Godbolt.
you can use /std:c++17 flag on MSVC and -std=c++17 on gcc/clang.

Related

Visual Studio 2017: ambiguous symbol size_t in linux projects

In Visual Studio 2017 when creating Linux project and inserting using namespace std; in source code like this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
size_t i = 1;
string s = to_string(i);
cout << i << s << endl;
return 0;
}
VS underlines size_t and says that it is an ambiguous symbol.
If I press F12 (Go to definition) it offers me two definition places:
From stddef.h
(C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\Linux\include\usr\include\x86_64-linux-gnu\5\include\stddef.h):
// ...
namespace std
{
typedef __SIZE_TYPE__ size_t;
// ...
And c++config.h
(C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\Linux\include\usr\include\x86_64-linux-gnu\c++\5\bits\c++config.h):
// ...
#if !(defined (__GNUG__) && defined (size_t))
typedef __SIZE_TYPE__ size_t;
// ...
It happens only with Linux projects in VS, not with Windows projects.
Is there any known solution (except for "do not use using namespace std; :) )?
Upd: Reported this problem to Microsoft: https://developercommunity.visualstudio.com/content/problem/67405/ambiguous-symbol-size-t-in-linux-projects-when-usi.html
Upd2: Microsoft says that they fixed it, and solution will be in next update: https://developercommunity.visualstudio.com/content/problem/67405/ambiguous-symbol-size-t-in-linux-projects-when-usi.html
It looks like there is a difference between Microsoft's and other compilers related to typedef in and out of a namespace.
This source file
namespace foo { typedef int moo; }
typedef int moo;
using namespace foo;
extern moo a;
compiles in g++ and clang++ (no warnings with -Weverything). MSVC rejects it because of an ambiguous symbol.
This is exactly the situation with size_t in gcc headers. It is typedefed both in and out of namespace std. This doesn't seem to cause any problems with
g++.
Why does this compile in g++ and not in msvc? I guess this is because of different interpretation of 7.1.3/3
In a given non-class scope, a typedef specifier can be used to redefine the name of any type declared in that scope to refer to the type to which it already refers.
Admittedly the interpretation by g++ is rather loose. The first moo is not declared within namespace :: so the rule seemingly doesn't apply. I cannot find anything else that would permit such a thing.
To solve the problem, I would patch the header where size_t is defined in the global namespace, and bring the declaration inside namespace std (conditionally, if __cplusplus is defined). But I have not tested it (no VC2017 here) and cannot guarantee it will work.
I also have no idea why your code is accepted by the actual compiler and only rejected by IntelliSense. I have tested this construction with actual compilers. (update to the last sentence: I have had the code tested with, and rejected by, MSVC. I have conducted the tests before realising that "the actual compiler" above is in fact gcc and not MSVC).

Error using alias declaration

I'm trying to compile an easy program that use the alias declaration.
This is the code:
#include <iostream>
using namespace std;
using in = int;
in main ()
{
in a = 1;
cout << a << '\n';
return 0;
}
The command I use to compile is g++ -std=c++0x program_name.cxx, using the built-in terminal in Kate on Ubuntu OS.
But it doesn't work! Any suggestion?
(instead using typedef int in; it works).
Compile in C++11 mode. Type aliasing is supported only in C++11. I suspect the g++ version that use is older and doesn't fully support c++11, hence fails with c++0x.
Compile with: g++ -std=c++11 file.cpp
and it works.
By the way, it seems to be a terrible idea to alias int in such a way.

Is there a method to use gmpxx.h together with c++98?

Because of my project I need to use c++98 and gmpxx.h:
But even for a simple project, it doesn't work:
#include <gmp.h>
#include <gmpxx.h>
int main()
{
int xrange=5,yrange=5,component=5;
return 0;
}
The error message is:
I tried using the following compiling methods
libc++: support c++11 and thus work
libstdc++: only support c++98 and do not work
Is there a way to use c++98 to implement gmpxx? thank you :)
Detail of errors when using c98++ to implement:
The breaking line is:
cout<<r<<endl;
But it works in c++11:
The error report:

Error compiling thread program in codeblocks 12.11

my thread program is :
#include<iostream>
#include<thread>
using namespace std;
void t()
{
cout<<"from thread\n";
}
int main()
{
thread i(&t);
cout <<"from main\n";
i.join();
}
but it shows following error in codeblocks:
1)'thread ' was not declared in this scope
2)expected ';' before 'i'
3)'i' was not declared in this scope
How can I solve it?I am using windows and codeblocks 12.11
I have the same problem. Unfortunately the version of GCC Code::Blocks uses does not support the features of C++11 that you want. Turning on the option -std=c++0x will only ugrade the compiler to a less recent version of the new Standard. That means you will only enable basic support of C+11.

c++ error std::result_of does not name a type

After g++ -std=c++0x'ing std::result_of produces the following error message
error: ‘result_of’ in namespace ‘std’ does not name a type
(g++ version 4.5.0 on SUSE.)
The relevant piece of code, sufficient for reproducing the error is below
#include <random>
#include <type_traits>
using namespace std;
class Rnd{
protected:
static default_random_engine generator_;
};
template<class distribution>
class Distr: Rnd{
distribution distribution_;
public:
typename std::result_of<distribution(default_random_engine)>::type
operator() (){ return distribution_(default_random_engine); }
};
Moreover, I have tried to compile examples from wikipedia or cpluplus.com to no avail.
Is it a problem with the particular compiler or am I doing something wrong?
Try to include <functional> also. gcc 4.5 is based on an older version of C++11, in which std::result_of is defined in <functional> instead of <type_traits>.
This move was introduced in n3090 (2010 March 29) after fixing issue 1270. gcc 4.5.0 was released just 16 days after the change (2010 April 14), which did not have enough time to apply, as we can see from this online source code of <functional>.
std::result_of was moved to <type_traits> in gcc 4.6.