The code below works unexpectedly!
class CameraBuffer;
class CameraBufferAccessor{
friend CameraBuffer;
private:
CameraBufferAccessor(const int &data, const int &mutex);
public:
CameraBufferAccessor() = default;
CameraBufferAccessor(const CameraBufferAccessor &) = delete;
CameraBufferAccessor(CameraBufferAccessor &&) = delete;
void operator=(const CameraBufferAccessor &other) = delete;
void operator=(CameraBufferAccessor &&other) = delete;
};
class CameraBuffer {
public:
const CameraBufferAccessor getReadAccess() const {
return {1, 2};
}
};
int main() {
auto buffer = CameraBuffer();
CameraBufferAccessor a = buffer.getReadAccess();
CameraBufferAccessor b = a;
here CameraBufferAccessor b = a;
as expected throws error since copy constructor is explicitly deleted.
but something happens that i donot expect and i have searching for it so far no explaianation. the line CameraBufferAccessor a = buffer.getReadAccess();
just works although it uses a function which has non-reference return, which itself is supposed to make use of Copy Constructor, secondly the returned CameraBufferAccessor
should be copied into a
initializing a
using Copy/Move constructor once again, but it compiles and doesn't throw.
I heard That the compiler for optimization purposes is allowed to bypass copy construction when necessary, if that's the case here, how am I supposed to tell when compiler does that?
I thought that it was due to the curly braces in return statement of getReadAccess
which directly initialized the return value, So I tried return CameraBufferAccessor(1, 2)
; but it still works. maybe compiler optimization?
Aucun commentaire:
Enregistrer un commentaire