« first day (1248 days earlier)      last day (1601 days later) » 

4:41 AM
Is this considered to be a undefined behaviour? *p += newValue; I saw this code on C++ Primer 5th and I think it's UB
 
 
2 hours later…
6:58 AM
@Rick It's conceivable that it would be UB if p is pointing to newValue, but otherwise it looks fine.
 
7:41 AM
I have a lot of trouble dividing concepts into projects and repos. I'm working on a project now and in the past a completely unrelated project I made a class I would now like to use. Is there a "correct" or recommended way of doing this? In terms of Git or the IDE (which is Netbeans)? Or should I just go ahead and copy and paste from the old project into the new one.
 
7:55 AM
if it's a single class I'd say just copy it and put a comment with a link to the source repo in there
 
thanks
 
@northerner Git does provide submodules, which are its way of dealing with situations like this--but they're kind of a pain, so they may not be worth the trouble for a single class.
 
@JerryCoffin tbh honest I find Git in general kind of a pain in the sense for even simple things like reverting back a commit it's not a single intuitive command.
 
@northerner git definitely isn't intuitive in general. Some of what it does is fairly complex, and its UI does make some simple things complex (though in fairness, it also makes some complex things pretty simple).
 
 
5 hours later…
12:39 PM
@JerryCoffin Ok I see. I just checked some common UB related to order of evaluation and didn't find a match. Well, when I doubted it , I was thinking of an "advice" from the book, which is :
> Advice: Managing Compound Expressions
> ............................
> 2. If you change the value of an operand, don’t use that operand elsewhere in the same expresion.
 
 
2 hours later…
nwp
2:19 PM
@JerryCoffin I believe they fixed that in C++17 and it should be valid even if p points to newValue.
Then again p could be nullptr and it's UB.
 
2:30 PM
p won't point to newValue
 
nwp
Why do you think it's UB then?
 
Ah. It was the rules written in the book that made me think it's UB...
As I quoted above.
 
nwp
The quote looks reasonable, but it says you shouldn't reuse operands when in the expression nothing is reused.
Actually I'm just waiting for someone to point out that it's a statement and not an expression.
 
LOL
nwp do me another favor please
I am having difficult in understanding this default arugment void reset( pointer ptr = pointer() ) noexcept(1)
pointer is a member type of unique_ptr
std::remove_reference<Deleter>::type::pointer if that type exists, otherwise T*. Must satisfy NullablePointer
So how should I understand this pointer ptr = pointer() default argument?
T* ptr = T*() ?
When the template type is int : int *ptr = int*() ? That doesn't work.
 
nwp
Yes. pointer is actually defined here.
And it's a typedef. using pointer = int*; pointer p = pointer(); compiles.
 
2:40 PM
Where did you know that it's a using poiter = T* ?
I didn't see that on the document QAQ.
 
nwp
It says it's a member type.
 
Ah I see. using or typedef
Ok. Then can I have a "raw" version of that pointer p = pointer(); ?
Why can't I write int *ptr = int*() ?
I think there should be a "raw" version statement ? (Doesn't everything that uses using or typedef have a "raw" version?)
 
nwp
@Rick My first guess is operator precedence, but that doesn't seem to be it. It probably says somewhere in the rules that that syntax is not allowed for some reason.
 
Also, I was told that pointers in C++ have (default) construct??
 
nwp
@Rick No. Typedefs are not macros. You can do things like using lli = long long int; and then void f(unsigned lli); which is valid, but lli is a variable name, you cannot compose them. There is no "raw" equivalent.
 
2:45 PM
really? that kinda shocks me. built-in types have the concept of constructor??
 
nwp
@Rick They are primitive types. They behave like they have a constructor and a destructor. You can even call the destructor of int using typedefs. It just doesn't do anything.
 
I asked a question here stackoverflow.com/questions/59286430/… and all I got are comments and "might be duplicated". That really pissed me off.
 
nwp
I don't know the wording exactly, but there are lifetime rules that say you are only allowed to access objects that have been constructed until they have been destructed. That applies to primitive types too.
 
Could you give me some sources (e.g. SO links) about how primitive types can act like custom class ?
using pointer = int*; pointer p = pointer(); that's really a "WOW" for me
 
nwp
Oh, and the answer to your question is probably "value initialization". int i; leaves i uninitialized, int i{}; makes it 0. int *p{}; makes p nullptr.
The reasoning is mostly templates. It would suck if std::vector<int> doesn't work because std::vector tries to call ~T() which doesn't exist for int == T. So they made that valid for builtins.
 
2:51 PM
Wow wow wow, that's a good point !
 
nwp
To me it's plausible, but if you look at Java you actually do have that problem. PriorityQueue<int> doesn't compile in Java because int doesn't inherit from Object. So they stuff an int in a class and call it Integer and use PriorityQueue<Integer>. Not sure which one is more or less weird.
Generally C++ has the goal to make classes and built-ins behave the same whenever possible.
 
Ok. I would delete that question and ask a new one.
I have more information now.
 
nwp
I guess they chose pointer ptr = pointer() over pointer ptr = nullptr because std::unique_ptr and nullptr were added at the same time. Due to the standardization process it makes sense to make your proposal not rely on other proposals, so the proposal for std::unique_ptr couldn't use nullptr and then nobody cared to change it in newer versions because it doesn't matter.
 
Yes, I saw this void reset( std::nullptr_t p = nullptr ) noexcept; since C++ 17
This is much easier to understand
 
nwp
You should watch some C++ WAT if you are ok with seeing a train wreck of a language in action.
 
3:01 PM
Let me live T_T
Mark and watch it maybe 10 years later :)
 
3:19 PM
@nwp check what I found
44
Q: Do built-in types have default constructors?

Prasoon SauravAfter reading this article I made a point that int () yields 0 because the temporary int is value initialized and not because int() calls the default constructor for int. (The article is flawed according to my understanding.) I also said that primitive (built-in) types don't have constructors. T...

Very interesting
 
nwp
> If it looks like a duck and quacks like a duck, then its very duck like.
Glorious.
 
3:43 PM
Hello everyone, I'm working on an exercise, and I would like to know what you think of my 'solution'. If this is the wrong place to ask please let me know, but I didn't know where else to ask.

Consider a class `WIP`, which is not designed by you. It's interface is well known, but there's no guarantee that `WIP's` next release uses the same data members. You're using:

```
class MyClass
{
WIP d_wip;
...
}

What would be the problem when using this and how can this be avoided by using a slightly different kind of composition?
 
@rezi-ReinstateMonica See the P_impl pattern
 
nwp
There is nothing special to do, no pattern to know. Use the interface and not the data members and everything will work fine. If the data members are implementation details then WIP should have made them private, so normally you can't screw this up.
 
@Mgetz this is what I wrote :)
 
@rezi-ReinstateMonica not quite? you're using the WIP class as a direct data member?
 
@Mgetz that's still part of the exercise, but I get the confustion..
 
nwp
3:48 PM
I don't see why you would use a PIMPL. That's for reducing compile times, it doesn't give you any advantage when it comes to using the class.
 
@nwp it does if the impl changes a lot in terms of ABI compat
 
@nwp could you elaborate a little, my C++ programming is improving but I find the compilation, linking libraries etc... part still difficult to grasp
 
nwp
Why would you screw around with ABI compatibility? That way lies madness and UB.
 
@nwp I don't, but if I don't have to recompile something that's nice too
 
nwp
@rezi-ReinstateMonica The way I understood it is that you must use WIP because it's useful. WIP has a known reliable interface, but the data members may change which may break your code. The way to handle that is to use the reliable interface only and to never access data members directly.
PIMPL doesn't help you with that. private does.
 
3:54 PM
@nwp Mhhh I get what you mean, the question clearly implies though that something must be changed:

"how can this be avoided by using a slightly different kind of composition" means there should be something?!
 
nwp
There is the theoretical case that WIP suddenly becomes very fat because of a std::array<int, 1000000> inside and therefore MyClass becomes fat too and suddenly you produce stack overflows. You can fix that with a std::unique_ptr<WIP>. That's all I can think of.
 
Mhh that's probably not what the prof had in mind.. But this is a extracurricular course so there's no grade or smth. Going to send the prof an email, just wanted to get some second opinion on this before I send 'stupid' questions
Thanks for the help both of you:)
 
4:13 PM
@GRRohman Okayyyy, been a (very much) bit busy.
Here is what I came up with:
```
bool traverse_and_display()
{
static char prev_number = 0;
// print only the first time
if(prev_number == 0)
{
std::cout << "The digits after removing duplicates: ";
}
char number;
// get a char from stream
std::cin.get(number);
// if a number, and unique
if(number != '\n')
{
// print it, update prev_number
if(number != prev_number)
{
std::cout << number << ", ";
prev_number = number;
}
// printing not complete
return true;
}
// if a '\n', format output
else
{
std::cout << "\b\b \n";
// printing complete
}
Damn, it removed the indentation.
Just ignore the first line of main.
 
 
2 hours later…
6:27 PM
What's a no-op mean?
I've seen that many times.
> The first statement is disallowed because it's a no-op
1
A: How does a pointer (built-in types) which gets constructed like a custom class work?

Rakete1111There is no need for the "raw" syntax as you put it, since pointers can be zero initialized with a 0. int *p = 0; // nullptr I guess it might be useful for template argument deduction, but we already have a syntax for that: (int*)0. There isn't really a need to add yet more syntax for somethin...

no-op == no operation? What does it mean then ?
 
6:39 PM
If a statement it's a no-op, then it's disallowed?
 
@Rick it's allowed it just does nothing
literally means no-operation
 
OK. no-op != not allowed
 
 
3 hours later…
9:46 PM
@JaideepShekhar you sure the code works ? It doesn't work for me.
 
 
1 hour later…
11:09 PM
Hey guys another quick question, my last two questions got answered but didn't get any upvotes. I tried to be as clear as possible and provided MWEs. Is there anything I'm doing wrong or is this just how SO works :P
 

« first day (1248 days earlier)      last day (1601 days later) »