#include <mutex>
int main()
{
constexpr static std::mutex my_mooootex;
}
I get the error of:
"Initialization is not constant"
On Microsoft Visual Studio Community 2022. std::mutex has a constexpr constructor, doesn't it?
It's a flaw in Microsoft's compiler.
Your code is valid. Any compiler reporting an error with the code you show is doing so incorrectly.
Related
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.
I am trying to compile the following code in Visual Studio 2017:
#include <future>
int main()
{
std::promise<std::reference_wrapper<int>> promise;
(void)promise;
}
However, I get the following error:
error C2512: 'std::reference_wrapper': no appropriate default constructor available
Whereas it compiles fine with GCC and Clang.
Is this is a definite bug in Visual Studio or is it a valid implementation of std::promise?
Looks like it is a known issue in MSVC's standard library implementation.
A simpler reproduction scenario:
#include <future>
struct NoDefaultCtor
{
NoDefaultCtor() = delete;
};
int main() {
std::promise<NoDefaultCtor> p;
return 0;
}
I suppose you do not need std::reference_wrapper<int>. There is the suitable overloaded template for std::promise available:
template<class R> class promise<R&>;
Therefore you can fix your code in Visual Studio 2017:
#include <future>
int main()
{
std::promise<int&> promise;
(void)promise;
}
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).
#include <iostream>
#include <atomic>
using namespace std;
typedef enum day{sun =0, mon, tue}day;
int main() {
atomic<day> a(sun);
cout<<a<<endl;
return 0;
}
The above code try to create a enum variable as atomic type. But i am getting the following error.
undefined reference to std::atomic<day>::operator day() const
Does atomic have no support for enum type? or any mistake in my syntax? I am using g++ compiler running on 32 bit ubuntu 12.0.4 machine. thank you.
I've compiled the same code with the online compiler that supports C++11 & C++14. I didn't get any issues.
atomic isn't available before C++11 standards.
For your reference: http://ideone.com/fork/Pe4gVt
#include <iostream>
#include <cstddef> //type_t
constexpr size_t sz = 5; //error here "Identifier constexpr is undefined"
int main(){
return 0;
}
I get this error at compile time: "Identifier constexpr is undefined." Am I forgeting something? Including something maybe?
It seems like one those stupid things that will make me facepalm when I notice...
I'm using Visual Studio 2012.