我想知道为什么
shared_ptr
没有隐式构造函数。这里提到了它没有的事实:Getting a boost::shared_ptr for this
(我已经理解了原因,但还是觉得这是一个有趣的问题。)#include <boost/shared_ptr.hpp>
#include <iostream>
using namespace boost;
using namespace std;
void fun(shared_ptr<int> ptr) {
cout << *ptr << endl;
}
int main() {
int foo = 5;
fun(&foo);
return 0;
}
/* shared_ptr_test.cpp: In function `int main()':
* shared_ptr_test.cpp:13: conversion from `int*' to non-scalar type `
* boost::shared_ptr<int>' requested */
delete &foo;
。 - curiousguy