How can I prevent a variadic constructor from being preferred to the copy constructor?

You can use some ugly SFINAE with std::enable_if, but I’m not sure it is better than your initial solution (in fact, I’m pretty sure it’s worse!): #include <memory> #include <type_traits> // helper that was not included in C++11 template<bool B, typename T = void> using disable_if = std::enable_if<!B, T>; template<typename T> struct Foo { Foo() … Read more

How are C++ array members handled in copy control functions?

This is what the standard says in 12.8 (Copying class objects). Copy construction: Each subobject is copied in the manner appropriate to its type: if the subobject is of class type, the copy constructor for the class is used; if the subobject is an array, each element is copied, in the manner appropriate to the … Read more

How to properly duplicate an object given its shared_ptr

std::make_shared is just a simple template function that creates the objects, passing all arguments to the constructor : template<class T, class… Args> shared_ptr<T> make_shared(Args&&… args) { return shared_ptr<T>( new T( std::forward<Args>( args )… ) ); } In your particular case : std::shared_ptr<Event> o = std::make_shared<Event>(*e); the object is copied. If your code is such : … Read more

Is C++’s default copy-constructor inherently unsafe? Are iterators fundamentally unsafe too?

C++ copy/move ctor/assign are safe for regular value types. Regular value types behave like integers or other “regular” values. They are also safe for pointer semantic types, so long as the operation does not change what the pointer “should” point to. Pointing to something “within yourself”, or another member, is an example of where it … Read more

In which situations is the C++ copy constructor called?

When an existing object is assigned an object of it own class B = A; Not necessarily. This kind of assignment is called copy-assignment, meaning the assignment operator of the class will be called to perform memberwise assignment of all the data members. The actual function is MyClass& operator=(MyClass const&) The copy-constructor is not invoked … Read more

Why does the implicit copy constructor calls the base class copy constructor and the defined copy constructor doesn’t?

That’s just the way the implicit copy constructor is defined (it wouldn’t make sense calling the default). As soon as you define any constructor (copy or otherwise) its normal automatic behavior is to call the default parent constructor, so it would be inconsistent to change that for one specific user-defined constructor.

Why is template constructor preferred to copy constructor?

As far as I know non-template function is always preferred to template function during overload resolution. This is true, only when the specialization and the non template are exactly the same. This is not the case here though. When you call uct u3(u1) The overload sets gets uct(const uct &) uct(uct &) // from the … Read more

Copying derived entities using only base class pointers, (without exhaustive testing!) – C++

This approach is the preferred way of copying polymorphic objects because it offloads the responsibility of determining how to copy an object of an arbitrary type to that object, rather than trying to determine it at compile-time. More generally, if you don’t know what the base class pointer points at at compile-time, you can’t possibly … Read more

I defined a non-copy constructor; will a copy constructor still be implicitly defined?

First, let’s clarify our vocabulary a bit. A default constructor is a constructor which can be called without any arguments. A copy constructor is a constructor which can be called with a single argument of the same type. Given this, a “default copy constructor” would be a constructor with a signature something like: class MyClass … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)