sono riuscito a scrivere una classe come questa, catturando questo in un lambda definito come attributo non-statico di detta classe:Capture questo attributo lambda all'interno di una classe su modelli vs classe non-template
#include <memory>
#include <iostream>
#include <functional>
struct S
{
S()
{
std::cout << "S::S()[" << this << "]" << std::endl;
}
std::string y_{"hi mate"};
int x_;
std::function<void(int*)> del_{[this](int *ptr)
{
std::cout << "Deleting ptr[" << ptr << "] this[" << this << "] this->y_[" << this->y_ << "]" << std::endl;
}};
std::unique_ptr<decltype(x_), decltype(del_)> unique_{&x_, del_};
};
int main()
{
S s;
}
Questo compila e sembra funzionare bene.
Tuttavia, con una classe template, che non funziona più:
#include <memory>
#include <iostream>
#include <functional>
template <typename>
struct S
{
S()
{
std::cout << "S::S()[" << this << "]" << std::endl;
}
std::string y_{"hi mate"};
int x_;
std::function<void(int*)> del_{[this](int *ptr)
{
std::cout << "Deleting ptr[" << ptr << "] this[" << this << "] this->y_[" << this->y_ << "]" << std::endl;
}};
std::unique_ptr<decltype(x_), decltype(del_)> unique_{&x_, del_};
};
int main()
{
S<int> s;
}
$> g++ -std=c++1y custom_deleter_template.cpp
~/test custom_deleter_template.cpp: In instantiation of ‘struct S::’: custom_deleter_template.cpp:9:3: required from ‘S< >::S() [with = int]’ custom_deleter_template.cpp:24:10:
required from here custom_deleter_template.cpp:15:35: internal compiler error: in tsubst_copy, at cp/pt.c:12569
std::function del_{[this](int *ptr) ^Please submit a full bug report, with preprocessed source if appropriate. See for instructions. Preprocessed source stored into /tmp/pyro/ccxfNspM.out file, please attach this to your bugreport.
Prima di presentare una bugreport (che non posso fare, hanno bloccato la creazione di account), è normale che non si compila, in base a ciò che dice lo standard?
Il compilatore è g ++ (Ubuntu 4.9.2-0ubuntu1 ~ 14.04) 4.9.2, utilizzato flag -std = C++ 1y. La stessa cosa accade con flag -std = C++ 11.
Gli errori interni del compilatore sono sempre un bug. Questo dovrebbe essere compilato come fa in [clang] (http://coliru.stacked-crooked.com/a/4e113357c1393eb6). – 0x499602D2
Posso riprodurre con GCC 4.9.2 su OS X. – Cornstalks
compila in clang :). – pyro