有没有办法可以访问shared_ptr:
e.g。
# Maximum number of representations
# double.base ^ double.max.exp
base <- mpfr(2, 2048)
max.exp <- mpfr( 1024, 2048 )
# This is where the big part of the 1.79... comes from
base^max.exp
## 1 'mpfr' number of precision 2048 bits
## [1] 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216
# Smallest definitive unit.
# Find the largest negative integer...
neg.ulp.digits <- -64; while( ( 1 - 2^neg.ulp.digits ) == 1 )
neg.ulp.digits <<- neg.ulp.digits + 1
neg.ulp.digits
## [1] -53
# It makes a real small number...
neg.eps <- base^neg.ulp.digits
neg.eps
## 1 'mpfr' number of precision 2048 bits
## [1] 1.11022302462515654042363166809082031250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e-16
# Largest difinitive floating point number less than 1
# times the number of representations
xmax <- (1-neg.eps) * base^max.exp
xmax
## 1 'mpfr' number of precision 2048 bits
## [1] 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368
identical( asNumeric(xmax), .Machine$double.xmax )
## [1] TRUE
当我运行程序时,执行bar后会得到一个segafult。 我确实理解了seg-fault的原因。
我的最终目标是使用弱指针然后回调。
由于
答案 0 :(得分:0)
y
的生命周期由共享指针管理,或者当它在终止时创建的堆栈帧时超出范围。下定决心并坚持下去。
如果此代码可能以某种方式工作,它会导致y
两次,一次在main
结束时超出范围,一次在最后一次shared_ptr
消失时消失。 / p>
也许你想要:
int main()
{
std::shared_ptr<Y> y = std::make_shared<Y>();
y->foo();
}
此处,实例在其最后shared_ptr
消失时被销毁。这可能发生在main
的末尾,但如果foo
松开shared_ptr
的副本,它可以延长对象的生命周期。它可以松开一个弱指针,也许在全局对象的析构函数中告诉对象已经消失。
弱指针的要点是能够将它提升为强指针,这可以延长对象的生命周期。如果没有办法动态管理对象的生命周期,这将无法工作。