This question already has answers here:
Assign a nullptr to a std::string is safe?
(4 answers)
Why is assignment of 0 or nullptr to std::string allowed, even when it results in a straight forward runtime exception?
(2 answers)
Closed 10 months ago.
I've been trying to introduce std::optional into my projects lately, and it's great! But now I'm a bit confused, look at the following code:
#include <iostream>
#include <optional>
#include <string>
using namespace std;
int main()
{
std::optional<string> s = nullptr;
return 0;
}
This code build normally without any warnings! This is bad, because if I accidentally assign nullptr to std::optional in my project, it will cause a panic, but I can't find it at compile time! There may be other ways to catch this error at compile time, but I don't know of them right now. Does anyone have any good ideas?
Related
This question already has answers here:
static const vs #define
(11 answers)
Closed 4 years ago.
Goal
In the end, I want to know if using #define is bad for your code, and why.
Code
#include <iostream>
using namespace std;
#define favouriteNumber 20;
int main()
{
int number = favouriteNumber;
cout << number;
}
According to Stroustrup it's particularly "bad" for defining constants because the compiler can't check types then.
On the other hand, if one line of macro saves you 20 lines of explicit code, some people will certainly agree that it's useful even if it's inherently unsafe. That's because writing more code usually implies higher probability for mistakes.
This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Does a const reference class member prolong the life of a temporary?
(6 answers)
Closed 5 years ago.
i have some sample code that behaves different in gcc(6.3) and in MSVC (11). I know that passing raw "hello" is not going to last when i call std::cout. In Microsoft Visual Studio 11 i get the expected output but in gcc the string remains in memory. The problem is that the code in gcc may compile, may behave as expected but is wrong, and i do not know why did not erase the "hello" string from memory. What i am missing? (Ignore the memory leaks is just dummy code)
Edit
The question is more oriented about the difference of compiling with gcc or MSVC. I have already pointed that the code will yield into undefined behaviour. The point is that something like this has been running ok under linux during years and has crashed at first run under a Windows port. Maybe that was only luck but i was asking more about gcc compiler doing nasty things in the background.
MSVC output
w_holder:
Gcc output
w_holder:hello
Code
#include <iostream>
#include <vector>
#include <string>
class StringWrapper
{
public:
StringWrapper(const std::string& sInput) : _string(sInput) { }
const std::string& _string;
};
class WrapperHolder
{
public:
WrapperHolder(const std::string& sInput) { _swrapper = new StringWrapper(sInput); }
StringWrapper* _swrapper;
};
int main()
{
WrapperHolder* w_holder = 0;
w_holder = new WrapperHolder("hello");
std::cout << "w_holder: " << w_holder->_swrapper->_string << std::endl;
return 0;
}
This question already has answers here:
Why is "using namespace std;" considered bad practice?
(41 answers)
Closed 5 years ago.
In my function sumofrange I decided to output an undeclared variable just to learn the different compiler errors in C++. To my surprise, time seems to output 1 even though it is not declared anywhere.
#include <iostream>
#include <cmath>
using namespace std;
int sumOfrange( int lower, int upper){
cout<<time<<endl;
return ((( (pow(upper,2)) + upper) - ((pow(lower,2)) + lower)) / 2);
}
int main(){
cout<<sumOfrange(7,100)<<endl;
return 0;
}
You are outputting the address of a std::time function declared in a <ctime> header. You are also using a using namespace std; statement. Why that should be avoided is explained in this SO post. Depending on the compiler and the platform you might get the hexadecimal output similar to (0x)00DC52E0 if using a VC++ compiler on Windows or a number 1 if using a g++ compiler on Linux.
This question already has an answer here:
Does not name a type
(1 answer)
Closed 5 years ago.
very easy but frustrating question from me.
#include <vector>
#include <iostream>
using namespace std;
vector <int> queue;
queue.push_back(2);
int main(){
cout << queue[0] <<endl;
}
compiled with g++ -std=c++11 Cpp_test.cpp, return error
Cpp_test.cpp:51:1: error: ‘queue’ does not name a type
queue.push_back(2);
Can anyone help? Thanks a lot!
queue.push_back(2); should go in main.
To clarify, you cant just place code arbitrarily and have it executed. Declarations are fine outside of main, but that's not a declaration.
Jay is correct. However, since you're using C++11, you can keep your "initialisation" near the declaration by actually making it an initialisation:
vector<int> queue = {2};
(live demo)
By the way, std::vector is a strange choice for a queue.
This question already has answers here:
How do I deal with the max macro in windows.h colliding with max in std?
(6 answers)
Closed 7 years ago.
This is rather bizarre, I have a class I've been building and currently I have this at the top of my file:
#pragma once
#include <cstdint>
#include <cstring>
#include <string>
#include <limits>
Now I need to add windows.h to the mix but as soon as I do that, I get "Error: expected an identifier" on this line:
inline uint32_t Hash2(std::string &Key) {
return (MurMur3::x86_32(Key.c_str(), Key.size(), 2) % (std::numeric_limits<uint32_t>::max() - 1)) + 1;
}
the red line appears under the ::max if that matters. As for the function itself, its supposed to use murmur3 to get me a hash that isn't 0.
If I remove
std::numeric_limits<uint32_t>::max()
and replace it with the constant 4294967295
then it works fine again.
I don't understand why this is happening. Does anybody have a clue?
Windows.h has a very bad habbit of defining macros in it. In particular, it defines min and max. You need to undef those.