4

I am after two things. I first want to typedef the vector class to something a little more meaningful such as List. I tried the following but it gave me a compile error:

template <typename T>
typedef vector<T> List<T>

Secondly I want to override the << operator of the vector class, but I have no idea how I would go about it without creating a new class.

This may seem counter-productive but my end aim is to have something that non-programmers (or people that haven't done c++ before) can read that makes semantic sense.

Cheetah
  • 13,785
  • 31
  • 106
  • 190
  • 3
    It is counter-productive. Everything about it. – Cat Plus Plus Apr 16 '12 at 12:02
  • As a programmer, yes its counter-productive...but to achieve my end aim, it is CERTAINLY not. – Cheetah Apr 16 '12 at 12:07
  • Why would none programmers read your code? If they're programmers who haven't learnt C++ it's better they learn C++ than that you hide the workings of the language from them. – Andreas Brinck Apr 16 '12 at 12:09
  • `std::list` is already another type of container, having a vector-type called list will be very confusing later on for your non-tech pupils. – RedX Apr 16 '12 at 12:12
  • It's not about the code, but at the same time I want my audience to be able to tell me "what comes next" or "find the problem"...one comment I have had so far is that they were confused as to what a vector was as they know what a vector is from basic maths principles...so I wanted to change it to List (which is what it is semantically representing in my case). @RedX: hence the capitalisation. – Cheetah Apr 16 '12 at 12:12
  • 1
    Then just use the type list and not a vector... – RedX Apr 16 '12 at 12:14
  • @RedX: I have to use `vector` for some functions and api calls that I didn't implement. – Cheetah Apr 16 '12 at 12:16
  • 3
    Or just use `std::vector` and don't rename it at all… In computer science, [a vector is a one-dimensional array](http://en.wiktionary.org/wiki/vector#Noun) (def 8) and your students should get used to it if they want to learn C++. –  Apr 16 '12 at 12:17
  • My audience is not comprised of only computer scientists, and my audience may not ever learn C++ or want to learn it to participate. You may ask why C++ and not a simpler language, well because there are some libraries which I am using which are C++. – Cheetah Apr 16 '12 at 12:19
  • 4
    @Ben It might still be more productive (it certainly would be helpful for your audience) to write a thin wrapper around the libraries you need to use, and use a simpler language. Putting C++ (in whatever form) in front of non-programmers is doomed to fail. It will be a lot of arcane shibboleth to them and they won’t learn anything. – Konrad Rudolph Apr 16 '12 at 12:23
  • My audience understand Pseudocode and the only thing they had trouble with was the vector name and push_back. (I'm guessing because they code contains actual mathematical vectors too). I wanted to do a live demo to show the results of the changes they suggested. Last time I did this I showed the code in notepad and did a regex replace all to change vector to List and push_back to << and they understood fine. I'd rather not do that as everytime they suggested a change I would have to change it in notepad and then again in the actual code to run. – Cheetah Apr 16 '12 at 12:28
  • 2
    @Ben: If they understand pseudocode, why don't you just show the changes in pseudocode, and never show C++ code at all ? This "almost-C++ thing" will not help your audience, but will surely confuse anyone that is used to C++. – ereOn Apr 16 '12 at 13:01
  • It's a live demo, I make the changes they suggest and run the code again so they can see what their changes did. I can't see how it would confuse anyone that is used to C++. For all they know, List is a class I have created?! and the `<<` is used in lots of things other than bit shifts, such as when writing to the std::cout. – Cheetah Apr 16 '12 at 13:38
  • Perhaps, your approach may be less confusing with more neutral mnemonic. For instance, why not to replace _List_ with _Sequence_ (indeed, both vector and list are sequence containers) or << with something like _add_element()_ ? – user396672 Apr 16 '12 at 14:32
  • There is nothing counter productive about making your code more readable! – Isaac Paul Aug 02 '14 at 11:24

2 Answers2

6

If you do this, non-programmers still will be unable to read, let alone change, the code. However, C++ programmers will also have a lot of trouble reading the code.

If they cannot code, and need a C++ programmer to code it for them, then they will need a C++ programmer to understand, maintain, and extend the code which that first C++ programmer has written.

If, OTOH, they need to code in C++, then — surprise! — they will have to learn to write and read C++ code.

There really is nothing in between.

sbi
  • 219,715
  • 46
  • 258
  • 445
  • To add to this, `List` and `Vector` are equally meaningless to someone who has no idea about programming. Like Sbi says, they either need to learn to program in C++ or they need to get someone who knows how to. You also need to consider what happens when some one starts to get into this programming and wonder why their code is so messed up. Just teach them properly from the start. – thecoshman Apr 16 '12 at 13:07
  • Quite to the contrary actually. My audience fully understood the C++ code when I used regex to change vector to List. I had non-programmers making the correct suggestions during the live demo. They never "code" as such, they simply tell me which variables to change, what variables need to be added to a list and so on...so no, actually they don't need to learn to write C++. The code is only there so they can a) see the full algorithm of whats going on and b) should the programmers wish to make major changes, they have the code to do so. – Cheetah Apr 16 '12 at 13:36
5

There are no template typedefs in C++, but you can use using instead in C++11.

template<class T>
using List = std::vector<T>;
// ...
List<int> foo; // aka std::vector<int> foo;

Operator overloading can be done without modifying the class.

template<class T>
std::vector<T>& operator<<(std::vector<T>& vec, const T& value) {
  vec.push_back(value); // or whatever you want to do.
  return vec;
}

Just put it somewhere and it should work, even outside of the class definition of std::vector.

  • 3
    Overloading `operator<<()` to add to a vector violates [the basic rules of operator overloading](http://stackoverflow.com/a/4421708/140719): ___1)___ Whenever the meaning of an operator is not obviously clear and undisputed, it should not be overloaded. ___2)___ Always stick to the operator’s well-known semantics. – sbi Apr 16 '12 at 12:24
  • @sbi I completely agree, but this is what the OP asked for. You may add that note to the answer if you wish. –  Apr 16 '12 at 12:26
  • I already _did_ add a note regarding this to the answer. `:)` (I didn't downvote, but I couldn't let this get by without pointing out that it will be hurtful in the long run.) – sbi Apr 16 '12 at 12:52
  • @sbi Boost violates this big time, with great effect. Now one could say “quod licet Iovi …” but Ruby also uses `<<` for appending to an array so it’s not unheard of. Not that I disagree entirely. – Konrad Rudolph Apr 16 '12 at 13:04
  • @Konrad: _Sigh._ I should really rephrase that rule. Everyone points out the three examples of libraries who violated this and got away with it. – sbi Apr 16 '12 at 13:05
  • 1
    @KonradRudolph imagine that you see a vector used with `operator<<` in real code. The first thing you would do is think "WTF?" *It is confusing.* –  Apr 16 '12 at 13:07
  • @sbi Reflex reaction. I saw afterwards that your linked answer already has a disclaimer. – Konrad Rudolph Apr 16 '12 at 13:09
  • @class Maybe but that’s not the case here. I’d see a non-standard class `List` using the operator, and would immediately try to familiarise myself with the class’ semantics. – Konrad Rudolph Apr 16 '12 at 13:09
  • 1
    @KonradRudolph And then you find out it's actually a vector and you are even more confused… –  Apr 16 '12 at 13:11