C++ type coercion deduction -


i'm playing around colvin-gibbons trick implementing move semantics in c++03 , i've got following:

#include <unistd.h> #include <stdio.h> #include <stdlib.h>  template <typename t> class buffer {     struct buffer_ref {         buffer_ref(t* data) : data_(data) {}         t* data_;     };  public:     buffer() : data_(null) {}     //explicit buffer(t* data) : data_(data)        {}     buffer(size_t size)      : data_(new t[size]) {}     buffer(buffer_ref other) : data_(other.data_) { other.data_ = null; }      buffer(buffer    &other) : data_(other.data_) { other.data_ = null; }    ~buffer()                                      { delete [] data_;    }      operator buffer_ref() { buffer_ref ref(data_); data_ = null; return ref; }     operator t*()         { return data_;                                    }  private:     t* data_; };   int main() {     buffer<float> data(buffer<float>(128));     printf("ptr: %p\n", (float*)data);  }    

edit: formatting

i'd able use buffer pointer base type when convenient, i've added casting operator pointer type, works expected. however, if uncomment constructor takes pointer, the conversion deduction gets confused , complains ambiguous conversion (because can go buffer->t*->buffer or buffer->buffer_ref->buffer). expect explicit modifier on pointer constructor fix this, doesn't. can understands c++ conversion deduction better me explain compilers thinking?

this direct result of 13.3.1.3 [over.match.ctor]:

when objects of class type direct-initialized (8.5), or copy-initialized expression of same or derived class type (8.5), overload resolution selects constructor. direct-initialization, candidate functions constructors of class of object being initialized. copy-initialization, candidate functions converting constructors (12.3.1) of class. [...]

because buffer<float> data(buffer<float>(128)); direct-initialization, have explicitly requested explicit constructors considered.

if write:

buffer<float> data = buffer<float>(128); 

then there no ambiguity.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -