Initialization at Declaration

Using the = at the declaration of an object is supposed to be the same as calling its constructor, right? I've come across this situation:

1
2
3
4
5
6
7
8
//Build(PartType) returns a pointer to RobotPart
std::unique_ptr<RobotPart> catalyst=Build(PartType::HUBBlock);
//This gives me an error:
//C:\...\Extensions.cpp|40|error: conversion from 'RobotPart*' to 
//   non-scalar type 'std::unique_ptr<RobotPart>' requested|

//But if I use this syntax, everything is all hunky-dory
std::unique_ptr<RobotPart> catalyst(Build(PartType::HUBBlock));


Aren't these supposed to be the same thing?
It would be, but the constructor for std::unique_ptr(pointer p) is explicit -- the constructor can't be called implicitly by operator=.

EDIT:
See this for reference (ctor #3): http://www.cplusplus.com/reference/memory/unique_ptr/unique_ptr/
Last edited on
Oh okay, now it makes much more sense to me. I never even considered the explicit keyword. Thanks!
Topic archived. No new replies allowed.