Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » C++0x, progress or pointless

This thread is locked; no one can reply to it. rss feed Print
C++0x, progress or pointless
anonymous
Member #8025
November 2006

The Wikipedia article quotes this usage:

template< typename LHS, typename RHS> 
  auto AddingFunc(const LHS &lhs, const RHS &rhs) -> decltype(lhs+rhs) {return lhs + rhs;}

Apparently you'll only need it when the return type would depend on the types of the arguments.

Trezker
Member #1,739
December 2001
avatar

Ok then. I think we've exhausted the {} debate for now.
How about we discuss something else that's new in C++0x?

I'm not gonna do the discussing though, I just think it's getting boring to follow the thread when you just argue over a single feature.

BAF
Member #2,981
December 2002
avatar

Can't you do that already with templates without that syntax though?

bamccaig
Member #7,536
July 2006
avatar

anonymous said:

Apparently you'll only need it when the return type would depend on the types of the arguments.

It makes sense, although that's probably the only time it should be used (and anybody that tries to use it unnecessarily should be shot).

Trezker said:

Ok then. I think we've exhausted the {} debate for now.
How about we discuss something else that's new in C++0x?

I'm not gonna do the discussing though, I just think it's getting boring to follow the thread when you just argue over a single feature.

They were actually just arguing about a different feature. ;)

BAF said:

Can't you do that already with templates without that syntax though?

If you don't know what the types are then how can you know what adding them together will give you? I think that's why this exists. So the compiler knows the return type is the result of adding these types together.

BAF
Member #2,981
December 2002
avatar

It seems like quite a stretch to me.

bamccaig
Member #7,536
July 2006
avatar

BAF
Member #2,981
December 2002
avatar

Please give a useful example of how it is useful then. The example anonymous gave is quite a stretch, how often does your return value depend on mroe than one template parameter?

bamccaig
Member #7,536
July 2006
avatar

BAF said:

Please give a useful example of how it is useful then. The example anonymous gave is quite a stretch, how often does your return value depend on mroe than one template parameter?

In C#/.NET, when you use the + or - operator with two System.DateTimes, the result is a System.TimeSpan.

Arthur Kalliokoski
Second in Command
February 2005
avatar

anonymous said:

there is no extra overhead if they don't want to have their POD initialized at this point.

What's POD? The closest definitions I could find were Portable On Demand and Prince Of Darkness.

They all watch too much MSNBC... they get ideas.

bamccaig
Member #7,536
July 2006
avatar

Arthur Kalliokoski
Second in Command
February 2005
avatar

Plain Old Data Structures? That's PODS.

They all watch too much MSNBC... they get ideas.

SiegeLord
Member #7,827
October 2006
avatar

If you don't use C/C++, you shouldn't be posting in this thread, because, clearly, you don't need the special features those two languages provide.

That article is yet another FQA-like bit of nonsense... except it's about C++0x. Yes, it's not perfect, but what other choice is there when you want to do numerical programming?

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Timorg
Member #2,028
March 2002

Any of these would also do the job I am sure, But Fortran 2003 comes to mind.

____________________________________________________________________________________________
"c is much better than c++ if you don't need OOP simply because it's smaller and requires less load time." - alethiophile
OMG my sides are hurting from laughing so hard... :D

SiegeLord
Member #7,827
October 2006
avatar

Yeah, FORTRAN is the only serious alternative to my knowledge, but I would scarcely say that it is all that much more user friendly than the C/C++ duo.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Arthur Kalliokoski
Second in Command
February 2005
avatar

C/C++/C++0x aren't in that list at all.

They all watch too much MSNBC... they get ideas.

BAF
Member #2,981
December 2002
avatar

bamccaig said:

In C#/.NET, when you use the + or - operator with two System.DateTimes, the result is a System.TimeSpan.

That's not a good example. I mean a useful example where you would even want to do something like the original snippet.

anonymous
Member #8025
November 2006

I remember a thread in Gamedev where someone wanted to make Vector<int> + Vector<double> return Vector<double> (or generally Vector<the_type_that_the_result_is_promoted_to>). It can be simulated (at least to some extent) with some metaprogramming, but C++0x would allow this quite simply.

It is probably not a very practical example, just something that a zealous library-writer thought would be cool.

Another thing is that perhaps it can provide a small shortcut to things that are tedious to type:

template <class T>
typename std::vector<T>::const_iterator foo(const std::vector<T>& vec);

template <class T>
auto foo(const std::vector<T>& vec) -> decltype(vec.end());

Ok, not that much shorter, and probably not entirely clear :) (This, I believe, would actually be simplified by template typedefs.)

It is quite likely that its main usefulness would be in template metaprogramming or with some new features like variadic templates.

In practice it's happening every time you add values of different type: they are promoted to something but you'll just assign (cast) them to some type to store the result.

-----

If I'm not mistaken, all the features don't come from some committee. Real programmers like you and me can (could) make suggestions, and then the committee would discuss which are actually good ideas, which are feasible to implement, which would break existing code etc.

Martin Kalbfuß
Member #9,131
October 2007
avatar

Quote:

But in C++0x you can write

auto someFunc() -> int
{ // function body...

It would be nice without the auto keyword. It looks more like a return value.

http://remote-lisp.spdns.de -- my server side lisp interpreter
http://www.nongnu.org/gm2/ -- Modula-2 alias Pascal++

Tobias Dammers
Member #2,604
August 2002
avatar

anonymous said:

I remember a thread in Gamedev where someone wanted to make Vector<int> + Vector<double> return Vector<double>

Someone who wants to do this should re-think their coding habits.
The proper solution is a utility function that takes one Vector<int> and one Vector<double> and returns a Vector<double>. Even with vectors of the same contained type, operator+ isn't intuitive - addition could be understood to mean any of:
- append all elements of A to C, then append all elements of B to C, then return C
- append all elements of A to C, then append all elements of B that are not elements of A to C, then return C
- for each element in A, find the element at the same position in B, calculate their sum, and append it to C; then return C

Facilitating something like this isn't cool, it leads to the kind of unmaintainable convoluted code the C folks are so proud of, or the kind of template abuse some C++ programmers seem to love so much.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Kibiz0r
Member #6,203
September 2005
avatar

If everyone is dead set against c/c++, why are we all (THarte Excepted) writing our allegro games in c/c++

If it weren't for allegro I wouldn't be using it at all

Allegro# 2.0 this summer. (Well, 1.9 until A5 officially comes out.)

Also, I'm not digging C++0x.

anonymous
Member #8025
November 2006

Someone who wants to do this should re-think their coding habits.

The question was about the geometrical vector, not the container.

I understand that it was an entirely theoretical discussion, since in practice it probably won't be that necessary, but C++0x would allow one to implement it like this:

template <class T, class U>
auto operator+(const Vector<T>& a, const Vector<U>& a) -> Vector<decltype(a.x + b.x)>
{
    return {a.x + b.x, a.y + b.y}; //another advantage of new constructor syntax?
}

Right now what you could do is:

//lots of metaprogramming here

template <class T, class U>
Vector<promote<T, U>::type > operator+(const Vector<T>& a, const Vector<T>& b)
{
    return Vector<promote<T, U>::type >(a.x + b.x, a.y + b.y);
}

Or perhaps support just a few vector types (who needs many different types anyway) and define the int + int -> int and double + double -> double versions (if the implicit conversions work, if not also int + double -> double and double + int -> double).

Tobias Dammers
Member #2,604
August 2002
avatar

I'd go for overloading just operator +=. This way, the return type is obvious, and it's up to the user to decide which return type they want:

template <class T> 
class Vector<T> {
   //...
   template <class U> 
   Vector<T> operator+=(const Vector<U> rhs) {
     x += rhs.x;
     y += rhs.y;
     return *this;
   }
}

Or ditch the overloading for incompatible types and use a static method:

template <class T> 
class Vector<T> {
   //...
   template <class U, class V> 
   static Vector<T> add(const Vector<U> a, const Vector<V> b) {
     return Vector<T>((T)(a.x + b.x), (T)(a.y + b.y));
   }
}

Or choose maintainability over cleverness and do everything with just Vector<double>, except at the very last stage, where you use Vector<int>. Then all you need is a templated copy constructor that can initialize a Vector<T> from a Vector<U>, and you're done.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

anonymous
Member #8025
November 2006

I'd go for overloading just operator +=.

But this is against convention. If the types support +=, they should also support equivalent + ;)

Quote:

Or ditch the overloading for incompatible types and use a static method

Except the calls would be rather awkward:

c = Vector<double>::add(a, b);

If one is writing a library to be used often, I'm all for the one-time investment to make things more pleasant for the end user.

Quote:

Or choose maintainability over cleverness

I've never said that the whole thing is meant to be practical... but isn't it nice if you can do it simply?

Tobias Dammers
Member #2,604
August 2002
avatar

I prefer languages that discourage impractical code.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

anonymous
Member #8025
November 2006

Todays C++ also discourages it.

In practice it seems that it can simplify your prototypes (a bit like I showed above, but instead with a out-of-line member function definition involving nested types as the return value) and it may have something to do with SFINAE (well, not my line).

I guess it is also a compromise with compiler implementators (if I'm not mistaken the standard committee is trying to avoid repeating the "export" mistake - a very useful feature which practically all compiler writers found too difficult to implement).



Go to: