Czasami temu widziałem kodu:Inteligentny wskaźnik Deleter i „za” słowo kluczowe z nazwą „wskaźnik”
void* Create()
{
int* t{new int{10}};
return t;
}
class Deleter
{
//uncomment in order to compile
//using pointer = void*;
public:
void operator()(void* t)
{
delete t;
}
};
unique_ptr<int, Deleter> ptr{Create()};
to nie kompiluje. Z VS2013 mówi:
error: C2440: 'initializing' : cannot convert from 'initializer-list' to 'std::unique_ptr' No constructor could take the source type, or constructor overload resolution was ambiguous
Ale gdybym Odkomentuj linia using pointer = void*;
to działa! Również jeśli zmienię nazwę aliasu na coś innego niż pointer
, pojawia się ten sam błąd. Tak więc wydaje się, że kluczowe znaczenie ma posiadanie dyrektywy using
o dokładnej nazwie pointer
. Ale dlaczego? Nie mogłem znaleźć żadnego wyjaśnienia.