« first day (522 days earlier)      last day (2337 days later) » 

11:37 AM
hi guys
I'm reading about the rule of three, five and zero
what's the meaning of the rule of zero?
"Classes that declare custom destructors, copy/move constructors or copy/move assignment operators should deal exclusively with ownership. Other classes should not declare custom destructors, copy/move constructors or copy/move assignment operators."
meaning of "ownership" specifically
 
11:54 AM
5 messages moved from Lounge<C++>
 
Ron
No way around C it seems. If someone has a fair understanding of C++, then how long do you think it would take to learn C?
 
I'd say it would depend from how you use C++
recents standards have almost nothing to do with C
 
The language itself? Not too long, read up on feature list of C99 and C11, differences on feature support, and the rest is basically a restricted C++
also a lot of parts that in C++ you'd rather pretend they don't exist
null-terminated strings, C arrays, function pointers and so on
 
Ron
@milleniumbug Yes. Appreciate it. I would like to move to embedded and it seems it's C only.
 
Embedded presents its own challenges because you'll see a lot of language extensions
 
Ron
12:09 PM
I see. I got spoiled with SO C++ pedantry so much that I wonder if it is possible to go with C at this point.
Anyways, as always, thanks for the feedback.
 
12:20 PM
Hi all, perhaps a silly question, too simple and maybe duplicated, but I can't get this to work:

int main()
{
  char* vars[2];
  for (int i = 0; i < 2; i++)
  {
    vars[i] = '0' + i;
    std::cout << " vars[" << i << "]: " << vars[i] << std::endl;
  }
}

The idea is to convert from int to char* in a loop
 
use fixed font button
@user8469759 If an object owns a resource, it's responsible for freeing it in the destructor
 
but is there a definition of ownership?
 
Sep 11 '16 at 12:21, by milleniumbug
No one likes C strings
C++ has a std::string type, use it
 
@milleniumbug is that for me? I need to pass a char* like that one to another function that I can't control
 
not an excuse
std::string s = "lol"; f(&s[0]);
 
12:27 PM
I will try that.
 
@iled char* vars[2];?
 
@user8469759 see the first article I linked
> An essential part of RAII is the concept of resource ownership: the object responsible for cleaning up a resource in its destructor owns that resource.
 
sbi
12:52 PM
@user8469759 Given that we all expect the owner of a resource to release the resource, I suppose a possible conclusion from this is that whoever cleans up after you used a resource is the owner.
 
 
2 hours later…
2:27 PM
Classes that have custom destructors, copy/move constructors or copy/move assignment operators should deal exclusively with ownership
does this mean then that if a class doesn't have pointers then it shouldn't have any constructor (copy/move) and assignment operators (copy/move)?
 
sbi
3:17 PM
@user8469759 It means that, if a class manages resources, each object should manage a single resource, and the class should do nothing else. If a class implements behavior, it shouldn't also manage resources, but delegate that to resource-managing classes.
 
3:28 PM
say I have this class cpp.sh/8gklhf
this class shouldn't implement any copy/move constructor/assignment operator, right?
because data members are allocated in the stack, and there's no need to manage their life time
same for this class
but in this case the object is movable and copyble
but in a case like this
I need to apply the rule of 5
because my object is neither copiable or movable, is that right?
however according to this: scottmeyers.blogspot.co.uk/2014/03/…
I should explicitly declerare all defaults move and copy costrunctors and assignment operators
@sbi all the stuff above was for you xD
 
3:56 PM
Please provide comments on IOC container using C, stackoverflow.com/a/47808315/3317808
 
 
2 hours later…
5:59 PM
hi guys
suppose I have a graph data structure, implemented using list of nodes for each node
namely each node has as a data member a list of the adjecent nodes
the latter would be a list of pointer
I was wondering
if in the spirit of C++11 what kind of smart pointer would be better to use here
my guess would be shared_ptr
 
you can trivially avoid pointers by using indices instead
 
not sure
if you have to manipulate the graph
you have to change indices etc
but anyway I was trying to come up with some "complicated" application of shared_ptr
 
7:00 PM
You don't want to use shared_ptr here, because any cycle would cause a memory leak
Graphs commonly have cycles.
Maybe there's a way to set it up so it doesn't cause a leak. Hmm
 
std::weak_ptr for breaking cycles, but it's not always feasible to set it up
 
Yeah I just realized that if you use adjacency lists with a raw pointer (equivalent to the shared) as the key for looking up the list, you might not have cycles of shared_ptrs. At any rate, it's harder to have a shared_ptr cycle than I first thought even if the graph has a cycle
There's no way I'd want to prove it though. It's likely much easier to just use indices / raw pointers.
 
I still stand by my statement for prefering indices
@user8469759 so?
 
I do too
 
indices make your graph trivially relocatable
 
7:11 PM
For choosing what kind of lifetime management you want, “Leak-Freedom in C++... By Default” by Herb Sutter is a really good talk.
 
7:31 PM
@user8469759 The talk Better Code: Runtime Polymorphism by Sean Parent does cover one use case of shared_ptr, although the talk is more about type erasure. The example was by using std::shared_ptr<const BaseClass> to have an immutable document; changes were made by "copying" the entire document and making changes, but shared_ptr makes the "copy" cheap, as it shares most of the state.
 
 
2 hours later…
9:45 PM
I have two enums:
enum PARAM_TYPE_ {INT_, STRING_};
enum PARAMETERS_ {NAME_, PARENT_NAME_, PARENT_, SIZE_};

and I want each of the elements of PARAMETERS_ to be either a INT_ or a STRING_ is there a smooth way to do that in C++?
 
a. @Felix.C that's very poorly worded
b. @Justin why on earth is that on godbolt. Were you going to inspect the assembly :)
 
@sehe Because it's an easy place to paste code and ensure it compiles
 
probably I have to ask more specific, thx Justin
I have a file and objects are described in that file, meaning there are multiple blocks with multiple lines where as the i-th line always has the same "meaning"...as I want to make it very easy to change number of arguments, I want to have an enum PARAMETERS_
Edit: Wait pls for the full question :)
where the elements in the enum are the parameters in each line. Each line contains a number or a text. to check whether file containing those lines has no syntax errors I want to check whether the lines represent the right "type" e.g. int or string...also since I want to make everything dynamically I want to use for(i = 0; i < SIZE_; ++i) to iterate over arrays that will be filled with the appropriate arguments, that doesn't work if I give them values in {0,1} as proposed in @Justin solution
 
boost::variant
 
9:58 PM
I need the elements in PARAMETERS_ to go from 0 to SIZE_ -1
 
These are parameters to what? A C++ function, or some code that you are interpreting or what?
 
These are attributes of elements in a UI
All elements described in the file are then instanciated in c++ where as the attribute NAME_ gives them a unique name, but I don't want to have the "attributes" as member fields but rather as a vector...perhaps a dumb idea...
 
It might make sense to have an enum (I'd use enum class) for the parameter type. But for the actual parameters, I wouldn't use an enum. I'd use some object that I can ask what its parameter type is
 
perhaps a bit too complicated, I just want to push_back a std::unique<Part>(new Part())) onto a vector for every element that has a description within the file
@Justin ok thx for the advice and @milleniumbug thx for the link
 
@Justin coliru, wandbox, ideone, rextester...
Most of them have far better library support.
 
10:08 PM
@sehe Yeah but I use godbolt more, so it's easier for me to think of it. It doesn't really matter to me unless I want to actually execute code / use other libraries
 
10:21 PM
Do I want to know what you do, if you usually don't want to actually execute code / use other libraries?
 
It's often just "see if this compiles" or "share code using some online service".
 
It so fun to try guessing what someone meant when they argued about something completely irrelevant and hold myself from asking a question about the reason for doing it.
 

« first day (522 days earlier)      last day (2337 days later) »