Why can a std::vector of uncopiable objects not be initialized with an initializer_list? - c++11

Why does this not compile? (it needs a copy-constructor for std::unique_ptr<>)
std::vector<std::unique_ptr<int>> vec{std:unique_ptr<int>(new int)};
The created pointer is an r-value so why can it not be moved into the vector?
Is there an equally short way of initializing the vector that works?

The simple problem is that std::unique_ptr's copy-ctor is deleted, but the construct of std:: that is found invokes ::new which triggers a call to that deleted copy-ctor.
libc++/clang++-3.5
/usr/include/c++/v1/memory:1645:31: error: call to implicitly-deleted copy constructor of 'std::__1::unique_ptr >'
template <class _Up, class... _Args>
_LIBCPP_INLINE_VISIBILITY
void
construct(_Up* __p, _Args&&... __args)
{
::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
}
libstdc++/g++-4.9
/usr/include/c++/4.9/bits/stl_construct.h:75:7: error: use of deleted function
‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&)
[with _Tp = int; _Dp = std::default_delete<int>]’
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
It somewhat tempting to try and formulate a custom overload of std::allocator as use it for the container's allocator type.
For those who cannot resist that temptation (like me)
To make a custom allocator at least used by the compiler it at least needs this minimum population.
template<typename T>
struct allocator : public std::allocator<T> {
using Base = std::allocator<T>;
using Base::Base;
template<typename U>
struct rebind {typedef allocator<U> other;};
template<typename Up, typename... Args>
void construct(Up* p, Args&&... args);
};
That is hardly complete yet (let alone would I have construct properly re-implemented yet), but at least it summons the error up from the abysses of the lib into the daylight a custom source file.

Related

Inferencing the typename of 'this' in a virtual method

I am aware of the lack of reflection and basic template mechanics in C++ so the example below can't work. But maybe there's a hack to achieve the intended purpose in another way?
template <typename OwnerClass>
struct Template
{
OwnerClass *owner;
};
struct Base
{
virtual void funct ()
{
Template <decltype(*this)> temp;
// ...
}
};
struct Derived : public Base
{
void whatever ()
{
// supposed to infer this class and use Template<Derived>
// any chance some macro or constexpr magic could help?
funct();
}
};
In the example, Derived::whatever() calls virtual method Base::funct() and wants it to pass its own class name (Derived) to a template. The compiler complains "'owner' declared as a pointer to a reference of type 'Base &'". Not only does decltype(*this) not provide a typename but a reference, the compiler also can't know in advance that funct is called from Derived, which would require funct() to be made a template.
If funct() was a template however, each derived class needs to pass its own name with every call, which is pretty verbose and redundant.
Is there any hack to get around this limitation and make calls to funct() infer the typename of the calling class? Maybe constexpr or macros to help the compiler infer the correct type and reduce verbosity in derived classes?
You should use CRTP Pattern (Curiously Recurring Template Pattern) for inheritance.
Define a base class:
struct CBase {
virtual ~CBase() {}
virtual void function() = 0;
};
Define a prepared to CRTP class:
template<typename T>
struct CBaseCrtp : public CBase {
virtual ~CBaseCrtp() {}
void function() override {
using DerivedType = T;
//do stuff
}
};
Inherit from the CRTP one:
struct Derived : public CBaseCrtp<Derived> {
};
It should work. The only way to know the Derived type is to give it to the base!
Currently, this can't be done. Base is a Base and nothing else at the time Template <decltype(*this)> is instantiated. You are trying to mix the static type system for an inheritance hierarchy inherently not resolved before runtime. This very same mechanism is the reason for not calling virtual member functions of an object during its construction.
At some point, this limitation might change in the future. One step towards this is demonstrated in the Deducing this proposal.

How to elegantly restrict a template argument to be a `<Certain_Class<AnyT>>`?

How to restrict a template argument of Wrapper to be a Wrapper<Any,MyArray<AnyT>> elegantly?
Don't break content assist (Visual Studio).
High readability. Not use a hacky approach.
For some reasons, most solution love to hack.
Make it obvious at the very first line by using C++ syntax rule. (not just green comment)
As far as I know, there are many solutions, but every solution doesn't meet the criteria.
Workaround 1 (template specialization, fail 1)
template<class T> class MyArray{};
template<class T,class T2> class Wrapper;
template<class T,class T2> class Wrapper<T,MyArray<T2>>{
using Test=int;
};
class B{};
class C{};
int main() {
Wrapper<C,MyArray<B>> wrapper;
return 0;
}
This code is modified from https://stackoverflow.com/a/43518221 (#max66).
Context clue / syntax highlighting of IDE will be confused.
In my case, it marks some correct types as error e.g. :-
class ShowError : public Wrapper<B,MyArray<C>>{
Test n=0; //<-- unknown "Test" (intellisense)
};
Workaround 2 (some hacky field/typedef, fail 2)
template<class T> class MyArray{
public: using MyArrayT=T;
};
template<class T,class T2> class Wrapper{
public: using myT=typename T2::MyArrayT;
//^ assert at compile time
};
This idea come from a comment in https://stackoverflow.com/a/43518295 (#Jarod42)
The class declaration doesn't mention about MyArray, it just uses a hacky (less readable) way (MyArrayT) to enforce that T2 is MyArray.
Workaround 3 (base class, fail 2)
class MyArrayBase{};
template<class T> class MyArray : public MyArrayBase{ };
template<class T,class T2> class Wrapper{
//check something around MyArrayBase *object = new T2();
// or "is_base_of"
};
The code is modified from Restrict C++ Template Parameter to Subclass and C++ templates that accept only certain types.
It has same disadvantage as workaround 2.
It is not obvious for common user.
Workaround 4 (SNIFAE, fail 1)
By adding std::enable_if on the template class declaration (Wrapper), I can get a working hack.
Unfortunately, content assist hate it.
Reference
Here are the other links that I read :-
http://www.informit.com/articles/article.aspx?p=376878 (template template parameter)
restrict a template function, to only allow certain types (not related to template type as a parameter)
You can write a custom type trait is_specialization, as follows:
template<class Type, template<class...> class Template>
struct is_specialization
: std::false_type {};
template<template<class...> class Template, class... TArgs>
struct is_specialization<Template<TArgs...>, Template>
: std::true_type {};
Then you just need to static_assert that is_specialization is true for the given template argument:
template<class T,class T2>
class Wrapper {
static_assert(is_specialization<T2, MyArray>::value, "T2 must be a specialization of MyArray");
};

copy constructor called instead of move constructor, why?

Consider this class:
template<typename T> struct pooled_resource: T{
template<typename... Args> pooled_resource(std::list<T>& pool, Args&&... args):
T(select_resource(pool, std::forward<Args>(args)...)), pool(pool){
if(!pool.empty()) pool.pop_front();
}
~pooled_resource(){
pool.push_front(static_cast<T&&>(*this));
}
private:
std::list<T>& pool;
template<typename... Args> static T select_resource(std::list<T>& pool, Args&&... args){
if(pool.empty())
return T(std::forward<Args>(args)...);
else
return std::move(pool.front());
}
};
It allows to create variables that have pooled resources but are semantically equivalent to the non-pooled version:
std::list<std::vector<int>> pool;
using pooled_vector = pooled_resource<std::vector<int>>;
{
pooled_vector a(pool); //pool is empty, allocate new resources
{ pooled_vector b(pool, 100); } //allocate again, but then release to pool as b goes out of scope
pooled_vector c(pool); //reuse b's resources
assert(c.size() == 100);
}
My actual problem is that for the simple case above, everything works and the move constructor of the pooled resource is called. However, I'm getting for another class that the move constructor is not called, but rather the copy constructor is. Precisely, the class is boost::compute::vector, that does declare a move constructor which seems to work in simple cases such as boost::compute::vector<int> a; boost::compute::vector<int> b(std::move(a));.
I have no clue why this happens and I don't know how to diagnose it: what am I missing regarding the rules for a move constructor to be actually used?
So the problem was a silly mistake in the class of the pooled resource. This is a sketch of it:
template<typename T, typename Allocator = DefaultAllocator> struct Resource{
/*...*/
Resource(Resource<T>&& o){ /*...*/ }
}
The problem was that the argument of the move constructor is of type Resource<T, DefaultAllocator>. Since I was using some custom allocator, there was actually no template move constructor available with a generic Allocator, but just with a generic type T and allocator DefaultAllocator. The fix just requires leaving out all template specifications of the move constructor:
Resource(Resource&& o){ /*...*/ }
Hope this can save somebody's else afternoon.

Possible to use SFINAE to pick between a shared pointer factory which uses make_shared vs shared_ptr constructor?

Background: I'm trying to create perfect-forwarding factory methods for creating shared pointers of classes, where it's very clear when someone is calling one that might have a side-effect by taking in a non-const lvalue as a constructor parameter. See: SFINAE enable_if for variadic perfect forwarding template on reference/pointer const-ness So far so good.
However, now my problem is that when using make_shared to create classes that have private or protected constructors, even if they friend my 'builder' class template instance for themselves, the inner make_shared doesn't have access to the constructor.
I had hoped I could SFINAE on that too, by making a wrapper class which itself tries to use make_shared to create a member variable in the same fashion as my builder plans to, and then use that in an enable_if<is_constructible<...>> but that returns true (in gcc 4.8.2) even if trying to actually instantiate such a class doesn't compile:
template<typename R>
struct builder {
class MakeSharedConstructTest
{
public:
template<typename... Args>
MakeSharedConstructTest(Args&&... args)
: m_R(std::make_shared<R>(std::forward<Args>(args)...))
{}
private:
std::shared_ptr<R> m_R;
};
};
struct NonPublic
{
friend class builder<NonPublic>;
private:
NonPublic() {};
};
. . .
// prints 1, ie yes, constructible
std::cout << std::is_constructible<builder<NonPublic>::MakeSharedConstConstructTest>::value << "\n";
// test.cpp:134:3: error: ‘NonPublic::NonPublic()’ is private
builder<NonPublic>::MakeSharedConstConstructTest q;
What am I missing here?

C++0x Member initialization without a constructor

In N3257 I found an example using initializing members without a constructor, which is fine. I guess that is possible, because it is a POD.
template<typename T>
struct adaptor {
NonStdContainer<T>* ptr; // <- data member
T* begin() { return ptr->getFirst(); }
T* end() { return ptr->getLast() + 1; }
};
void f(NonStdContainer<int>& c) {
for (auto i : adaptor<int>{&c}) // <- init
{ /* ... */ }
}
When I played around with this example I replaced the * with a &, because I don't like raw pointers:
template<typename T>
struct adaptor {
NonStdContainer<T>& ptr; // <- data member, now REF
T* begin() { return ptr->getFirst(); }
T* end() { return ptr->getLast() + 1; }
};
void f(NonStdContainer<int>& c) {
for (auto i : adaptor<int>{c}) // <- init
{ /* ... */ }
}
This was fine and compiled without warning with GCC-4.7.0.
Then I got curious about the initialization of PODs and what might have changed with C++0x.
There I found Bjarnes FAQ. He says there that PODs may contain pointers, but no references.
Ops, now I wonder:
Do I have non-POD-object here, which the compiler can initialize without a constructor anyway and I just miss which mechanisms are used here?
or Is the GCC-4.7.0 behaving non-std by letting me initializing the ref this way?
or has there been a change in the std since Bjarnes FAQ that also allows references in PODs?
Update: I found aggregates in the current std (8.5.1 Aggregates [dcl.init.aggr]), but references are not mentioned there, so I am not sure how they relate to this
Quoting the standard [dcl.init.aggr]:
An aggregate is an array or a class (Clause 9) with no user-provided
constructors (12.1), no brace-or-equal- initializers for non-static
data members (9.2), no private or protected non-static data members
(Clause 11), no base classes (Clause 10), and no virtual functions
(10.3).
When an aggregate is initialized by an initializer list, as specified
in 8.5.4, the elements of the initializer list are taken as
initializers for the members of the aggregate, in increasing subscript
or member order. Each member is copy-initialized from the corresponding initializer-clause...
That means you have an aggregate here, aggregates can be initialized how you do it. PODs have nothing to do with it, they are really meant for communication with eg. C.
Copy-initialization of a reference with a variable is certainly legal, because that just means
T& ref = c;
Do I have non-POD-object here, which the compiler can initialize without a constructor anyway and I just miss which mechanisms are used here?
Yes, the object is non-POD.
Is the GCC-4.7.0 behaving non-std by letting me initializing the ref this way?
No.

Resources