回复: 回复:答复: question about 'ZeroSpreadedTermStructure'

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

回复: 回复:答复: question about 'ZeroSpreadedTermStructure'

rage-3
Very enlightening , thank Alexander.
C++ has parsing ambiguity between a function declaration and a constructor invocation when the constructor arguments are temporaries. This is known as the "most vexing parse" problem.

So my problem has nothing to do with template arguments as I suspected. It is all because of '''temporaries''' arguments.

Here 'Handle<YieldTermStructure>(ftpTerm)' is the '''temporaries''' arguments.
So bracketting '(Handle<YieldTermStructure>(ftpTerm))' tells the compiler this is a reference to the temporaries, not a function pointer 'Handle<YieldTermStructure>(*)(ftpTerm)'. So this a a constructor invocation rather than a function declaration.

Above explains Cheng's first solution.
Cheng's second solution is just avoid '''temporaries'' by passing concrete objects.

By the way, clang's diagnostics is much better than the gcc's.

Regards!
Rage

------------------ 原始邮件 ------------------
发件人: "alexandre p";<[hidden email]>;
发送时间: 2014年12月27日(星期六) 下午4:56
收件人: "rage"<[hidden email]>;
抄送: "quantlib-users"<[hidden email]>;
主题: Re: [Quantlib-users] 回复:答复: question about 'ZeroSpreadedTermStructure'

This is known in C++ circles as "the most vexing parse". Wikipedia has an article on it: http://en.wikipedia.org/wiki/Most_vexing_parse

It always comes as a surprise but as Cheng points out an extra pair of parentheses resolves the ambiguity for the compiler.

Best regards

Alexander

On Dec 27, 2014 3:50 AM, "rage" <[hidden email]> wrote:
cheng, thanks for your response.

Indeed, the problem comes from the constructor calling. The key point is the ZeroSpreadTermStructure's constructor has template arguments(Handle<>), so the compiler may have trouble when parsing this constructor.

After posting this question, I've fixed it by changing:
    ZeroSpreadedTermStructure discTerm(Handle<YieldTermStructure>(ftpTerm), Handle<Quote>(spread))
    with:
    ZeroSpreadedTermStructure discTerm = ZeroSpreadedTermStructure(Handle<YieldTermStructure>(ftpTerm),
            Handle<Quote>(spread));

Your solutions work well. Could you elaborate on the c++ trick you mentioned? Why did you bracket the first parameter of the constructor function?


------------------ 原始邮件 ------------------
发件人: "cheng li";<[hidden email]>;
发送时间: 2014年12月25日(星期四) 下午2:00
收件人: "rage"<[hidden email]>; "'quantlib-users'"<[hidden email]>;
主题: 答复: [Quantlib-users] question about 'ZeroSpreadedTermStructure'

Hi Rage,

 

This is a c++ trick. The following line:

 

ZeroSpreadedTermStructure discTerm(Handle<YieldTermStructure>(ftpTerm), Handle<Quote>(spread));

 

Actually declare a function named discTerm and with parameters as Handle<YieldTermStructure> and Handle<Quote>, while return type as ZeroSpreadedTermStructure;

 

To avoid compiler to do such silly thing, you can change the line as follows:

 

ZeroSpreadedTermStructure discTerm((Handle<YieldTermStructure>(ftpTerm)), Handle<Quote>(spread));

 

Or

 

Handle<YieldTermStructure> curveHandle(ftpTerm);

Handle<Quote> quoteHandle(spread);

ZeroSpreadedTermStructure discTerm(curveHandle, quoteHandle);

 

Regards,

Cheng

 

发件人: rage [mailto:[hidden email]]
发送时间: 20141225 0:10
收件人: quantlib-users
主题: [Quantlib-users] question about 'ZeroSpreadedTermStructure'

 

Hi,

Since class ZeroSpreadedTermStructure is derived from YieldTermStructure, it should have methods like discount(). I have a simple example, when compiling it screams,

error: request for member ‘discount’ in ‘discTerm’, which is of non-class type ‘QuantLib::ZeroSpreadedTermStructure(QuantLib::Handle<QuantLib::YieldTermStructure>, QuantLib::Handle<QuantLib::Quote>)’

 

 

// code snippet:

    std::vector<Date> dates(4);

    dates[0] = Date(1,Jan,2014);

    dates[1] = Date(1,Feb,2014);

    dates[2] = Date(1,Jun,2014);

    dates[3] = Date(1,Dec,2014);

 

    std::vector<Rate> yields(4);

    yields[0] = 0.1;

    yields[1] = 0.2;

    yields[2] = 0.3;

    yields[3] = 0.4;

 

    DayCounter dayCounter = Actual360();

    boost::shared_ptr<Quote> spread(new SimpleQuote(0.01));

    boost::shared_ptr<YieldTermStructure> ftpTerm(new InterpolatedZeroCurve<ForwardFlat>

            (dates,yields,dayCounter,Calendar(),ForwardFlat()));

 

    ZeroSpreadedTermStructure discTerm(Handle<YieldTermStructure>(ftpTerm), Handle<Quote>(spread));

 

    std::cout<<discTerm.discount(1.0, true)<<std::endl;

---------------

what's the problem?

 

Regards!


------------------------------------------------------------------------------
Dive into the World of Parallel Programming! The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users


------------------------------------------------------------------------------
Dive into the World of Parallel Programming! The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users