C++ constexpr error - c++

#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.

Related

Why can't this mutex be constexpr constructed?

#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.

expected initializer before 'std'

#include <bits/stdc++.h>
using namespace std;
int32_t main()
{
cout << "Hello\n";
}
This is my code and the code is running, but Visual Studio Code shows this squiggles line with the message
expected initializer before 'std' gcc
which makes me conscious about my code.
Change int32_t to int
And never use #include <bits/stdc++.h>.

Why MSVC C++ requires a default constructor in struct needed when returning from async? [duplicate]

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;
}

Error "function count_if() could not be resolved" - C++

I am using Eclipse for C++ testing and I am getting an error related to the count_if() function.
Here is my code:
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> v{3,4,5,6,7,8,9,10,100,12,2};
auto isodd=[](int x){return x % 2;};
std::count_if(begin(v),end(v),isodd(3));
return 0;
}
While compiling this program Eclipse gives the following error: "function count_if() could not be resolved".
Thanks for any kind of hint.
I have done very silly mistake.By adding #include < algorithm>
I've solved my problem.

int_main()_error_'::main' must return 'int'

#include <iostream>
#include <stdio.h>
using namespace std;
int main(){
cout<<"Just work";
return 0;
}
i dont why it is giving the error '::main' must return 'int'
I am giving the return type and value
IDE: Eclipde Mars
There is no error in your code. The return type of your main is int as it should be.
The static analyzer of eclipse cdt is not very good at parsing c++. If you don't like reading false positive error messages, then you should disable it.