C ++ function definition and variable declaration mismatch?

Consider this very simple code:

#include <memory>

class Foo
{
public:
    Foo() {};
};

class Bar
{
public:
    Bar( const std::shared_ptr<Foo>& foo ) {}
}; 

int main()
{
    Foo* foo = new Foo;
    Bar bar( std::shared_ptr<Foo>( foo ) );
    return 0;
}

Why Visual Studio Reports

warning C4930: 'Bar bar(std::shared_ptr<Foo>)': prototyped function not called (was a variable definition intended?)

and no object is created bar... how can this line Bar bar( std::shared_ptr<Foo>( foo ) );be interpreted as a function definition?

I checked Does it contain parentheses after the type name with a new one? as well as C ++: warning: C4930: prototyped function not called (was this a variable definition?) , but I feel like my problem is different here since I did not use the syntax Foo()and Bar().

Edit: note that it successfully compiles:

Foo* foo = new Foo;
std::shared_ptr<Foo> fooPtr( foo );
Bar bar( fooPtr );
+4
source share
1 answer

++ . :

Bar bar( std::shared_ptr<Foo>( foo ) );

bar, bar foo std::shared_ptr<Foo>.

. :

Bar bar( std::shared_ptr<Foo> foo);

, ++ 11 ( std::shared_ptr), :

Bar bar(std::shared_ptr<Foo>{foo});

bar bar, - .

+14

Source: https://habr.com/ru/post/1685671/


All Articles