« first day (1289 days earlier)      last day (1560 days later) » 

2:58 AM
@Vaillancourt at the end I think probably I should just use compiler flag /permissive- instead of trying to include iso646...
 
 
6 hours later…
nwp
8:29 AM
@LanYi You shouldn't need to include anything to use and and xor. Those are keywords. Unless you are using C.
 
 
1 hour later…
user7659542
9:42 AM
uint8_t a = 5;
uint8_t b = 10;

uint8_t c = a + b;
 
user7659542
Considering integer promotion. Does the above addition cause an integer promotion to int, which is then casted back to an uint8_t?
 
user7659542
ie 2 casts
 
what difference does it make? Even if the standard mandated it, the as-if rule would make those casts a no-op for any even half-decent compiler
 
user7659542
because I see people doing this:
 
user7659542
uint8_t c = (uint8_t) a+b;
 
9:46 AM
Hello
 
user7659542
wondering why they specify that cast
 
@traducerad might be their compiler giving them a warning for narrowing conversion
 
How do you format code in chat again?
 
user7659542
@PeterT so there are actually 2 casts going on?
 
How do you fix repetition in situations like this?
`for(int i = 0; foo(i) < max; i++)
{
auto x = foo(i);
//do stuff with x
}`
 
9:49 AM
@traducerad don't know on top of my head and I'm too lazy to look it up now :P
 
Can you do this?
`int x;`
`for(int i = 0; x = foo(i) < max; i++)`
 
I think "=" has lower precendence than "<"
so I don't think that does what you think it does
 
I recall in Java there's some trick in these type of situations
 
nwp
There are 2 casts, yes, but the manual C cast doesn't help. [A C cast binds stronger than `+`](https://en.cppreference.com/w/cpp/language/operator_precedence), so it is parsed as `
uint8_t c = ((uint8_t)a)+b;` which means the cast does nothing as `a` is already a `uint8_t`. You'd need to do `
uint8_t c = (uint8_t) (a+b);` for it to even suppress a possible warning. For what it's worth the standard specifies that there must be modulo arithmetic for this, so it's perfectly legal without any casting.
Actually scratch that. I meant implicit conversions, not casts.
Yup, < binds stronger than = so you have to do (x = foo(i)) < max. If that is what you mean.
 
@PeterT can you do something described here in C++ or does it only work with objects? softwareengineering.stackexchange.com/questions/232993/…
int c;
while((c = fgetc(stream)) != EOF) {...}
 
nwp
9:59 AM
C++ considers an int an object.
 
user7659542
@nwp do you think it makes sense to cast? uint8_t c = (uint8_t)(a+b);
 
user7659542
I thought the cast was maybe used to avoid having the integer promotion
 
nwp
The integer promotion still happens.
 
user7659542
IIRC you have an integer promotion all the time for every addition, substraction etc you do
 
user7659542
@nwp ok so this is absolutely useless
 
nwp
10:01 AM
@traducerad I think the cast makes the code worse, not better and wouldn't do it. I'd turn off the loss of precision warning instead.
 
user7659542
@nwp loss of precision when going from int to uint8_t?
 
nwp
But I can see some people very worried about integer overflow and using it as a code guideline to always explicitly overflow to catch implicit unwanted overflows.
 
or just do the math in int32_t and then downcast at the end
assuming overflow isn't an issue
 
nwp
@traducerad Yeah. Some ints don't fit in a uint8_t and effectively get cut off which is probably wrong which makes tools warn about this.
 
user7659542
@ratchetfreak overflows with inttypes are very nasty imo
 
user7659542
10:02 AM
because you will probably not notice them
 
nwp
Well, you can tell compilers to warn about that I think. And in the rare case you actually want to overflow you cast.
 
user7659542
they are nasty becuase they silently wrap around. WHile when overflwoing with a normal int, you suddenly get crazy values
 
user7659542
@nwp that s useless, because at the end you still assign the value to a uint8_t c
 
overflow on signed integer is undefined behavior
 
user7659542
which is an implicit cast
 
10:05 AM
so yeah anything can happen
 
nwp
@traducerad Or UBSan steps in and aborts the program with a traceback. :P
 
user7659542
speaking about code analyzers
 
user7659542
is clang's static code analyzer good?
 
user7659542
ie complete and bulletproof
 
user7659542
@nwp
 
nwp
10:09 AM
I haven't messed with the static analyzers beyond what IDEs do automatically. The sanitizers though are worth it. They don't catch everything, but they catch a few things and have very few false positives.
 
 
2 hours later…
12:03 PM
I was debugging a program and didn't get the message 'std::bad_alloc' until I terminated the program (ctrl+c). Why is this? Is it any different from getting the error message prior to forcing the termination?
 
nwp
12:46 PM
Probably because the bad alloc didn't happen until termination.
Maybe a destructor is doing bad allocations. Or maybe the debugger didn't catch it correctly. Hard to tell.
 
1:34 PM
@nwp I'd use a static cast not a C style cast to indicate narrowing
 
 
1 hour later…
2:41 PM
@traducerad Relevant: this and this
 
 
2 hours later…
scx
4:38 PM
```
#include "pch.h"
#include <iostream>
using namespace std;
const wchar_t* gh()
{
return L"\0";
}
int main()
{
wstring s(gh());
const wchar_t* hell = gh();
cout << hell<<endl;
cout << "Hello World!\n";
}```

Ideally I can return NULL from the function ,however it will cause exception in wstring s(gh()); I should make change only in this function as it is referenced in many places in both the ways wstring s(gh());
const wchar_t* hell = gh();
So I wanted the best practice to follow in my scenario on what could be returned from the the function gh
 
You might want to hit the "fixed font" button that appears when you paste your code. Also, other kind of formatting does not work when using multi-line text here.
 
scx
@Vaillancourt Sry posting question for the first time
```
#include "pch.h"
#include <iostream>
using namespace std;
const wchar_t* gh()
{
	return L"\0";
}
int main()
{
	wstring s(gh());
	const wchar_t* hell = gh();
	cout << hell<<endl;
    cout << "Hello World!\n";
}```

Ideally I can return NULL from the function ,however it will cause exception in wstring s(gh()); I should make change only in this function as it is referenced in many places in both the ways wstring s(gh());
    const wchar_t* hell = gh();
So I wanted the best practice to follow in my scenario on what could be returned from the the function gh
 
Could you return a std::wstring instead of a wchar_t*? Aside from that, the last time I had that issue, I checked for null on the call site, which you seem unable to do here, so the help I can provide here is limited...
 
scx
@Vaillancourt I will try changing,thanks
 
@scx Looks like you're stuck with finding a workaround to not pass nullptr to your string ctor...
24
Q: Assign a nullptr to a std::string is safe?

bardesI was working on a little project and came to a situation where the following happened: std::string myString; #GetValue() returns a char* myString = myObject.GetValue(); My question is if GetValue() returns NULL myString becomes an empty string? Is it undefined? or it will segfault?

 
scx
4:54 PM
@Vaillancourt Looks like this will resolve my problem,thanks a lot
 
@scx :)
 
5:13 PM
Is there a way to debug a code that I already executed? To clarify my question, I have a code that works based on some randomly generated numbers, and right now I have a bug, but it only happens in some cases, randomly... Now, if I try and just debug my code it might be the random case where it works, is there a way so that I can compile my code, run it, see that the output doesnt work, and then debug that .out file?
 
seed your random number generator and log the seed
then you can rerun the runs that fail
 
@ratchetfreak That would seem satisfactory to my problem, thanks! Out of curiosity, is it possible to do what I specifically asked (even if not needed in my current situation)?
@ratchetfreak Do you know how to do this with std::random_device as I am using std::random_device and std::mt19937 and I am totally not familiar with these
 
5:34 PM
Hello
How can i browse file using c++ on mac os ?
 
5:53 PM
@NaranmandakhTsogoo Perhaps this could be your entry point?
 
Can i integrate something like NSOpenPanel
 
6:54 PM
I am researching formatted input using e.g., int ii; std::cin >> ii; If I enter invalid input in a loop, like "34 k" or ctrl-z (EOF) it loops continuously, no longer waiting for input even though the input code is inside the loop. Has anyone else witnessed such behaviour?
 
7:40 PM
@NaranmandakhTsogoo Oh, that, I wouldn't know.
 
 
2 hours later…
9:36 PM
Sorry, when I posted my question above, I was on my phone. Here is a minimal test case:
#include <iostream>

int main()
{
int ii = 0;

do {
std::cout << "Enter something: ";
std::cin >> ii;
} while (ii != -1);
}
Sorry about the formatting - I tried to indent the code 4 spaces like Markdown, but it did not end up as code. I did not find a button to make the code a fixed font, until I edited, then it was too late to change it. Any helpful suggestions?
#include <iostream>

int main()
{
  int ii = 0;

  do {
    std::cout << "Enter something: ";
    std::cin >> ii;
  } while (ii != -1);
}
OK, got it!!
Not sure why the fixed font is not there until I edit...
If I enter "k" it just endlessly loops, printing out "Enter something: " over and over again
This is on Visual Studio 2019 on Windows 10
 
10:55 PM
What's wrong with this code which is supposed to insert a value to a sorted array? :/

bool Insert(int* List, int& listSize, int value)
{
    for (i = 0; i < listSize; i++)
    {
        if(value > List[i])
        {
             for (j = 0; j < listSize; j++)
             {
                  List[j+1] = List[j];
             }
             List[j] = value;
             return true;
        }
    }
    return false;
}
 
11:50 PM
Realized didn't declare i and j. :P
 

« first day (1289 days earlier)      last day (1560 days later) »