tags:

views:

142

answers:

6

Hi all, i have examined code of RDcode. And i come across a defined function and i dont understand. Can you help me to this code.

template <typename T>
   class argless {
   public:
     argless(const T& c) : container(c) {}; // i dont understant this line.
     bool operator() (unsigned int v1,unsigned int v2){
       return container[v1]<container[v2];
     }
     const T &container;
   };
+7  A: 

It's a constructor that takes a reference to a const T, and initialises the member variable container with it.

Oli Charlesworth
@Oli: Please change "const reference to a T" to "a reference to a const T"
Armen Tsirunyan
@Armen: Good spot.
Oli Charlesworth
@Armen: They mean the same thing, by convention, because you can't have a "real" const reference.
GMan
@GMan: I actuall agree with Armen. The new wording is more consistent with descriptions of pointers.
Oli Charlesworth
@Oli: That's fine and true, but either is correct. It's less clumsy and more common to say const reference, though.
GMan
@Gman, @Oli: I partly agree with GMan, but only partly. It is indeed less clumsy to say, for example, "this function takes its argument by const reference". And the same applies to pointers, i.e. I don't see any problem when one says "the function takes a const pointer" meaning a pointer to const. These are technically incorrect, but for me personally acceptable (but I don't like it). But saying a fully qualified thing as "const reference to T" is really inappropriate since it is no less clumsy than "reference to const T"
Armen Tsirunyan
@Armen: But pointer to const and const pointer have two different and valid meanings, so it's important which you use. Contrarily, const reference is invalid and so we take it to mean reference to const, so using either one works perfectly. I'm not disagreeing with the form you suggested it, but I disagree it should have be changed. (Again, minor point; the change is correct too.)
GMan
@Gman: We both understand perfectly well the difference between the two, as to whether they are acceptable or not is a matter of personal taste. Any professional has to know the difference, and not get mixed up. However the notion of the difference between the two usually/sometimes comes to students not near the beginning of their C++ path. A typical scenario is when a student finally learns that there is a difference, but gets mixed up because "the more common" way to name things is technically wrong. And you must explain to them "well yeah we say this but mean quite the contrary..."
Armen Tsirunyan
+3  A: 

It uses initializer syntax to store the container passed in by const reference in the member variable container. It has to use initializer syntax, because the container member variable is a reference (it must be initialized via an initializer list).

Michael Goldshteyn
+3  A: 

This is a constructor that uses initializer list.

Donotalo
thanks to your answer, donotalo. it's a great help to me.
cmuse
@cmuse: you're welcome. :)
Donotalo
A: 

This is a class template for a functor that allows comparison of arbitrary elements in an indexable container, ie. one that implements memberOfT& operator[] (unsigned int).

In addition memberOfT must supporting bool operator<(const memberOfT&, const memberOfT&), otherwise the operator() that makes this a functor will not compile.

The line you are asking about simply makes an input container of class T available to other members of the class. Since this is held in argless as a reference, it's important that the source container used on the constructor of argless remain in scope for the duration of argless's use.

Steve Townsend
A: 

It is the syntax for 2 types of stuff:

1) Initializing a const variable.

2) Calling The constructor's base class.

Green Code
"Calling its parent's constructor" - what are you talking about?
Oli Charlesworth
Calling parent's constructor, take a few keys, write some code, see it's result! and then come here and vote down!
Green Code
@Oli Charlesworth: he means that the initializer list syntax can be used *also* to call the constructors of base classes, although in this precise example there are no base classes.
Matteo Italia
Well, if the answer is rewritten to make that clear, then I'll remove the downvote...
Oli Charlesworth
Everything else put aside I have a strong feeling that a person who has answered a question should not vote another answer to the same question down. Let others do it. If you don't like something, leave a comment and let others decide. I know it is not the convention, but somehow I think it should be (after being a week or so on stackoverflow)
Armen Tsirunyan
@Armen: That's up to you! If I see a bad answer, I'll vote it down, and leave an explanation. If the answer is fixed, I'll remove the downvote. I don't see why it matters whether I've already answered.
Oli Charlesworth
@Oli: Don't take it personally this thought has been haunting me for a couple of days now, I thought this was a good place to express it. It's just that stackoverflow has established a somewhat competitive system of answers, that is, different answers compete, and when competitors themselves vote each other down is wrong for me. Again, I do know this isn't the general approach, so consider this as just an opinion, no offence
Armen Tsirunyan
@Armen: none taken! But you're right, answering can become competitive. But I think we have to take it on trust that people aren't gaming the system by maliciously downvoting good answers.
Oli Charlesworth
@Armen: Perhaps you could make a suggestion on meta.stackoverflow.com (e.g. to disable downvoting for those who've already answered) to see what the "community" think.
Oli Charlesworth
@Oli: Good idea, I think I will, thanks :)
Armen Tsirunyan
@Armen: Actually, I've taken a look, and it has already been discussed several times (e.g. http://meta.stackoverflow.com/questions/22771/the-answer-to-tactical-downvoting-problem).
Oli Charlesworth
@Oli: Thanks, that was an interesting read. As a matter of fact I am against prohibiting answerers from downvoting. It shouldn't be a technical ban IMO, rather a moral one. I mean, obviously tactical downvoting, as mentioned in your link, is not a big issue. But the moral aspect is this: there is a question, you provide an answer which you THINK is right and other people provide answers which THEY think are right. Since we are all human and can be wrong, it is best if other viewers judge the answer(er)s, not the answerers themselves. That's my point. Anyway this issue has been discussed :)
Armen Tsirunyan
Oh, now i get it, Thanks Matteo. I meant base class (parent in inheritance).
Green Code
A: 

It is used to enable operator() to act like operator< for the components of another container. You would use it to sort one container based on the contents of another, where the first contains indexes into the second. In this case, you would gain the knowledge of what a sorted container looks like, without actually having to sort it - a useful thing if moving the elements around is expensive or impossible, such as strings.

DeadMG