c++ templates - is this possible?

This topic is 4767 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

say i have a class like this:
template<class num_t>
class quantity
{
public:
template<class result_num_t, class other_num_t>
quantity<result_num_t> operator*(const other_num_t& other)
{
return quantity<result_num_t>(_value * other.getValue());
}
num_t getValue(){return _value;};

private:
num_t _value;
};


Is there any way to get the compiler to deduce the return type of operator* ? Now I believe I could write:

quantity<A> a;
quantity<B> b;

quantity<ABproduct> result = a.operator*<ABproduct>(b);


but the syntax is horrible and I have to explicitly state the return type, which isn't always convienient (such as in compound statements). Note: I've got boost installed and I'm prepared to delve as deeply into template 'magic' as is needed if this is possible. [Edited by - Nitage on July 29, 2005 9:42:04 AM]

Share on other sites
No, it's not possible. C++ will not do type deduction on the return type of a template function.

Edit: what you can do is return a type the relies on both types, like make a template type
template <typename A, typename B> ProductType

and return that or a type typedefed inside that type.

Share on other sites
Any way around this or recommendations on how I could improve the function?

At the minute I'm just using the tpye of the left hand argument.

As an aside, is there a proposal to change this in C++0x?

Share on other sites
AFAIK, there are no plans to change this in the next version of the C++ standard.

Share on other sites
Ok - slightly different question:

If I have two types, say A and B, is there any expression which gives me the return type of A*B?

Share on other sites
Quote:
 Original post by SiCraneAFAIK, there are no plans to change this in the next version of the C++ standard.

I thought the 'auto' keyword was being thought about for this exact reason?

auto iter = myvec.begin();

The compiler has enough information, it just doesn't expose it through the language right now. This is one of the proposals that had a lot of support last time I checked. Has this changed? Also I haven't read it entirely, so I'm not sure if it includes templated functions or not, but as the types are replaced at compile time, I can't see how template functions provide any more of a problem that a normal function.

Share on other sites
Quote:
 Original post by NitageOk - slightly different question:If I have two types, say A and B, is there any expression which gives me the return type of A*B?

I'm using an older version of something that was in the boost sandbox for that, but I think it's been replaced by boost::mpl. I currently use something that looks like this:

boost::result_of_plus<A, B>::type
boost::result_of_multiplies<A, B>::type

and you can specialize the result_of structs for your own custom types as well. If you'd like the header I can send it to you, but it did not make it into any official boost release. Again, I think the functionality got folded into boost::mpl. Someone else may know for sure.

Here's an example (It's pretty ugly though).
template <typename LHS, typename RHS>inline typename boost::result_of_plus<LHS, RHS>::type	operator +(const LHS &lhs, const RHS &rhs){	typename boost::result_of_plus<LHS, RHS>::type nrv(lhs);	nrv += rhs;	return nrv;}

Share on other sites
Quote:
Original post by mfawcett
Quote:
 Original post by SiCraneAFAIK, there are no plans to change this in the next version of the C++ standard.

I thought the 'auto' keyword was being thought about for this exact reason?

auto iter = myvec.begin();

the OP doesn't want the assigned-variable to be dependent on the function's return-type, he wants the function's return type to be dependent on the assigned-variable. more like:

auto my_function(int a)
{ ... }

Object b = my_function(3);

where my_function would somehow know to return a type 'Object'.

even if possible through some magic, i believe this would violate the more fundamental rule where a "over-loaded function cannot differ by only its return type."

illone

Share on other sites
Quote:
 Original post by illonethe OP doesn't want the assigned-variable to be dependent on the function's return-type, he wants the function's return type to be dependent on the assigned-variable. more like:

Yes, of course. My misunderstanding.

Share on other sites
When num_t, result_num_t, and other_num_t are supposed to be the same you can ditch result_num_t, and other_num_t and just use num_t.
It wouldn't even need to be a templated function, only a templated class.

But if you want to be able to multiply a 'quantity' by a 'foo_bar' then just write an explicit operator function for it and make foo_bar a friend, again without templates, making the return type obvious.
So basically you have to specify a * operator function for each type you can multiply 'quantity' by.

1. 1
2. 2
JoeJ
18
3. 3
4. 4
frob
12
5. 5

• 13
• 16
• 13
• 20
• 13
• Forum Statistics

• Total Topics
632188
• Total Posts
3004650

×
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.25744596123695374, "perplexity": 2762.802628799061}, "config": {"markdown_headings": false, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2018-34/segments/1534221216718.53/warc/CC-MAIN-20180820160510-20180820180510-00613.warc.gz"}