Leading typename, “dot template,” and why they are necessary.

Anyone dealing with templates will eventually run into something like this

// templated class with a type alias for "type"
template <typename T>
struct Obj {
    using type = int;
};

template <typename T>
void f() {
    Obj<T>::type var; // should be "int var;"
}

But wait, there’s an error on the only statement in f()

# clang-3.6
error: expected ';' after expression
    Obj<T>::type var;
       ^
       ;

# gcc-5.1
error: need ‘typename’ before ‘Obj<T>::type’ because ‘Obj<T>’ is a dependent scope
     Obj<T>::type var; // should be "int var;"

gcc’s error is great in this case, telling us we need to add a leading typename to make it:

typename Obj<T>::type var;

The leading typename tells the compiler that Obj<T>::type is a type. But why do we need this? We know that Obj<T> is a dependent type because it depends on T.

First, you have to realize that without a leading typename, the compiler assumes (in the case of a dependent type) that it’s a static member variable, meaning the following is parsed correctly

template <typename T>
struct Obj2 {
    static constexpr int type = 5;
};

template <typename T>
void f2() {
    int n = Obj2<T>::type * 3;
}

So what? Why can’t the compiler just look inside of Obj and see that it has a type member. Well, in the case of template specializations, it can’t. Consider this:

// general case has "using type = int"
template <typename T>
struct Obj {
    using type = int;
};

// specialization for int has "int type = 2"
template <>
struct Obj<int> {
    static constexpr int type = 2;
};

Inside of f() we don’t know which one of these we’re dealing with. f() means it’s using the general template, but f() uses the specialization. The programmer must clarify that they expect a type.

You might be thinking “right, but in this case it’s clear that the body of f() is declaring a variable.” Right you are, but it’s not always this clear. What if instead we had

int x = 5; // global variable x
template <typename T>
void f() {
    Obj<T>::type * x;
}

Now we have two possible versions of this

int * x; // declaring a local variable x of type int*
2 * x; // multiplication expression

Both of these lines make sense, but they mean very different things. It’s not always clear which way it should be parsed.

“Dot Template”

You’re less likely to see this problem, but I think it’s cool and has the same idea behind it. When your dependent name has a templated member function, the compiler gets confused about what you mean again:

struct Obj {
    template <typename U>
    void m();
};

Now let’s create a variable with dependent type Obj<T> and call m()

template <typename T>
void f() {
    Obj<T> v;
    v.m<int>();
}

But hold on errors! this time clang being more helpful

# gcc-5.1
error: expected primary-expression before ‘int’
     v.m<int>();

# clang-3.6
error: use 'template' keyword to treat 'm' as a dependent template name
    v.m<int>();
      ^
      template 

Huh? template keyword? What’s happening is the compiler doesn’t know that m is a templated member function, we need tell it

v.template m<int>();

Alright so what’s the alternative here. Well, it’s a pretty well known issue with the C++ grammar that without context, you don’t know what certain lines mean. Borrowed from the D docs, consider the line:

A<B,C>D;

This could mean two things, (1) A is a templated class, B and C are template arguments, and D is a variable declared with that type. for example:

template <typename T, typename U> struct A;
using A = int;
using B = int;

A<B,C> D; // A<int,int> D

Or (2), A, B, C, and D are all variables and this is two comparisons separated by a comma

int A{}, B{}, C{}, D{};
(A < B), (C > D);

So, by default the compiler is assuming that m is not a template, which means that it assumes the angle brackets are less than and greater than operators, so it’s parsed as

((v.m) < int) > ();

This could make sense in a different context

template <typename T>
struct Obj {
    int m;
};

And then the following:

int a{};
v.m<a>0;
((v.m < a) > 0

So the “dot template” is needed to say that what follows is a template. Like I said this is far less common, and the syntax is so awkward that it can drive design decisions. If you’ve ever wondered why std::get is a free function, a big part is that std::tuple is often used as a dependent type.

template <typename... Ts>
void f(std::tuple<Ts...>& t) {
    std::get<0>(t); // nice
    t.template get<0>(); // gross
}

Canon Pixma MG5420 on Linux Mint

Another round with this new platform. My mom uses a desktop computer with a Canon Pixma printer/scanner, and I wanted to get this to work. I found a thread on askubuntu which referred me to the cannon europe site. There I downloaded the debian package listed as MG5400 series IJ Printer Driver Ver. 3.80 for Linux (debian Packagearchive). I untar’d and cd’d into the directory, but when attempting to run ./install.sh, I got the error:

dpkg: dependency problems prevent configuration of cnijfilter-mg5400series:
 cnijfilter-mg5400series depends on libtiff4; however:
  Package libtiff4 is not installed.

This is a problem with the old canon drivers looking for an old libtiff. Mint has libtiff5, but no libtiff4 in its repositories. I was able to grab a libtiff4 deb archive from the debian repos. I’m using an amd64 processor as most people so in my case I needed libtiff4_3.9.6-11_amd64.deb. After getting that and running sudo dpkg -i ../libtiff4_3.9.6-11_amd64.deb I was able to continue by running the canon ./install.sh normally.

Getting the scanner to work was a little surprising. It doesn’t work with xsane or simple-scan, but when I got MG5400 series ScanGear MP Ver. 2.00 for Linux (debian Packagearchive) from that same canon-europe site, it worked. Again, I got the tarball, unpacked it, ran the ./install.sh (this time without a problem). From there I had to run the command scangearmp in the shell, which opened up the program and let me start scanning images. As I said, I wanted my mom to be able to use this, so I created a desktop launcher to start it (and used /usr/share/icons/Mint-X/devices/48/scanner.png for the icon).

Mint and Cinnamon has my stamp of approval

I was looking for a distro to put on my girlfriend’s parents desktop. They’re not very tech-savy and have had their fair share of malware and virus issues using windows for years. My first choice was Ubuntu since it’s what I’ve used in situations like this in the past. However, their computer was a fairly old eMachines that Ubuntu couldn’t live boot with for some reason. Now, I’m sure I could’ve gotten Ubuntu to install, but given the users of the computer, I really wanted something that I was sure would work with minimal modifications for a long time. So, I took a shot and made a Linux Mint 17.1 (Rebecca) disc.

Linux Mint booted with no complaints and installed painlessly. Everyone found Cinnamon intuitive, simple, and easy. However, Firefox was freezing, not on the live boot, only after the install. I looked around and found this forum which said to switch to gdm. This worked.

The package manager and software center was also really straight forward. Since my goal was for this to relieve the endless headaches of windows without too much of a learning curve, I was happy. I installed it as a dual boot on my dad’s lenovo laptop he mostly uses for web browsing and it’s running smoothly there too.

When I need to install an easy-to-use linux distro, I’ll be reaching for Linux Mint pending some disaster.

P.S. Unity confuses new users too much to be worth their trouble most of the time.

Universal print syntax for Python 2 and 3

Often I see python programmers in python2 writing code like

print ('Hello World')

claiming “this will work in both python 2 and 3!” This is a noble pursuit, but the approach is misguided. In python3 the above behaves as a call to the print() function with a single argument, in python2 it is a print statement followed by a parenthesized expression. Once there are two arguments though, it fails

print ('Hello', 'World')

Under python3 this works as expected, but under python2 there is only one object following the print, and that object is a tuple. This will print the tuple as-is

('Hello', 'World')

So this piece of common misinformation is easily dismissed, the above is by no means universal since we can’t have multiple arguments. A real attempt at a universal print function means there can only be one object that is printed. The next step is to use join.

print (' '.join(('Hello', 'World')))

Great! call join with a tuple of ‘Hello’ and ‘World’, the separator space will do what printing normally does, then wrap that in parentheses to get back to step one (something that works with the print function). We also get the ability to use a custom separator when python2’s print only allows a space. It’s starting to seem like a lot of different things happening and that leaves room for human error. But there is a bigger problem. What happens when something other than a string is passed?

print (' '.join(('Hello', 'World', 2)))

join only works with strings, so now we have the error

TypeError: sequence item 2: expected string, int found

but that’s easily fixed by tossing a generator expression inside the join call to convert each item in the tuple to as string.

print (' '.join(str(e) for e in ('Hello', 'World', 2)))

Voila! a cross-python print function in just 5 easy steps

  1. put all of your arguments inside a tuple
  2. wrap that in a generator expression to convert them all to strings
  3. put that inside a call to ' '.join()
  4. wrap that with parentheses
  5. instert a print in front

Though there are two problems that remain: printing without a newline and printing to a file. Printing to a file can be achieved by changing stdout during the print call. Assuming we have a file object f

out, sys.stdout = sys.stdout, f
print (' '.join(str(e) for e in ('Hello', 'World', 2)))
sys.stdout = out

Change stdout to be f during the print call, then change it back after. I would strongly recommend putting that inside of a context manager (which exists in python3.4 stdlib) in case printing raises an exception.

If you can figure out a way to equate end='' with a trailing comma let me know.

Though at this level of complexity you might as well be using sys.stdout.write since getting equality means throwing out most of the convenience that print offers.

What you should take away from this is that there isn’t a simple way to write print syntax that works with both python2 and python3. If it’s available, in python2 I generally recommend

from __future__ import print_function

so you can just use the real python3 print syntax in python2. If it’s not, then write your own print function. If none of this is an option just use one or the other!. 2to3 can convert someday if you ever need to.

If you’re considering using what I’ve demonstrated here, please realize that this post is meant to show that trying to accomplish a universal print syntax is a mess. Don’t do this in real code.

Is the proposed fold syntax for c++17 too limited?

After reading this paper, and this cppreference page, and then posting a stack overflow question about it. I went ahead and built the latest clang from source to start messing around with fold expressions. Writing a sum() function is straight forward.

template <typename... Ts>
auto sum(Ts&&... args) {
  return (args + ...);
}

The syntax is nice, but imo a bit limited. The next thing I wanted was a variadic min() function. This is where the proposed syntax starts to seem weak. Obviously the outline of the function should be easy:

template <typename... Ts>
auto min(Ts&&... args) {
  return // something
}

but there’s no builtin operator in C++ that takes two things and returns the lesser of the two. It’s a pretty easy function to write, but it doesn’t work with the fold syntax. It surprises me that folds would be proposed without a way to have them work with functions. They’re functional in origin, after all.

Here is my workaround

template <typename T>
struct MinWrapper {
  const T& obj;
};

template <typename T, typename U, typename V=std::common_type_t<T,U>>
MinWrapper<V> operator%(const MinWrapper<T>& lhs, const MinWrapper<U>& rhs) {
  return {lhs.obj < rhs.obj ? lhs.obj : rhs.obj};
}  

template <typename... Ts>
auto min(Ts&&... args) {
  return (MinWrapper<Ts>{args} % ...).obj;
}

Okay, the MinWrapper is just a struct that binds a reference. This just gives me a type to define an operator on. After that I define operator% just because modulus seems to be the thing people use when they’re doing something terrible. The mod operator compares the items in each wrapper, and returns a wrapper with their common type. This actually does work in clang’s current head believe it or not.

My question is: if I thought of (nearly) this monstrosity before I even had a compiler in front of me to run it with, is this going to be a pattern that emerges once folds make it into the language for real? Probably.

The solution would be to extend the syntax to allow not only operators, but binary functions as well, this would let me write

template <typename... Ts>
auto min(Ts&&... args) {
  auto sel_min =
    [](auto&& lhs, auto&& rhs) -> auto&& 
      {return lhs < rhs ? lhs : rhs;};
  return (args sel_min ...);
}

not the prettiest lambda either, but imo, much better than what I’m doing with the current syntax.

Thoughts on TAs biggest mistakes as a TA and as a Student.

I’m by no means an education expert and not the greatest TA, but I try to be pragmatic in how I approach students, grading, everything. I’ve noticed there are some pretty common faults that I’ve really hated and many TAs don’t seem to care about. A lot of this may appear to be just about grading, but the grading aspects hint at deeper issues. I’ve never seen a good TA who was bad at grading.

-5 for everything

It seems that there’s a huge issue with TAs not realizing that they can take off points in anything other than 5 point increments. You forgot to capitalize your last name in the submission file? -5. Your answer is 1000 words when I asked for a sentence? -5. You missed one specific detail on a test question but were otherwise right? -5. Your presentation consisted of you reading words off of your slides for 10 minutes? -5. You turned in your homework assignment 2 days late? eh, -5.

The motivation for the minor points is this: if the submissions instructions for an assignment say to submit “lastname_firstname_proj1.txt”, and 5 kids submit “proj1.txt”, another submits “Untitled.txt”, and the last gives me “smith_john.pdf”, there’s a bunch of stuff that I need to address here. It’s frustrating. I hate having to take off points for stupid submission mistakes but every TA you talk to will tell you “If you tell them they messed up, but don’t take off points, they’ll just do it again.” Trust me, we all wish we could just inform students of their mistakes on these things and that be the end of it, but 10/10 it’ll happen again unless there’s some point loss.

What I’ve found, however, is that it doesn’t matter too much how many points are taken off, as long as it’s not 0. In this regard, -5, -10, -1, are all equally effective. You might not expect it, but students do respond to -1. What I usually do is take off -1 for each error unrelated to the actual body of the project. -1 per error allows the losses to have little-to-no effect on the students actual grade, but it also lets me be fair. If one student gives me “smith_proj1.txt” and the other gives me “proj1.pdf” clearly these are not both -5 violations. The first student might get -1 for missing their first name (a fair deduction imo), and the second could lose -3 for no firstname, no lastname, and the wrong file type.

Not talking

I would think this should be obvious, but I’ve had TAs who are silent in lab sections. There’s a less-common problem with some international students whose spoken English skills are shaky, where they seem very hesitant to speak to students at all for fear of sounding bad. I understand where they’re coming from (je parle français comme une vache espagnol), but I don’t know how to address the problem other than asking, please try.

(Nearly) all or nothing grading

This isn’t as common, but I see a lot of TAs who don’t actually understand what partial credit means. If there’s a 20 point question, I know TAs who will automatically subtract 15 for it not being exactly the correct answer. Somehow they think this is much better than grading without partial credit, but when people get in this habit, scores are either complete, partially complete, 0. This sucks.

If you have 20 points to work with, use the full range. A lot of it has to be relative.

“Does everyone understand this?” / “Does anyone have any questions”

This is the hardest thing to get around, and honestly, I commonly can’t get around this problem myself. If you say anything like this to a class, 95% of the students who would have questions won’t ask them. The only time I will flat out ask students if they have any questions is if I’m teaching a new concept for the first time, and I want to get feedback early (I aim for at least every 5 minutes). Like I said, it’s hard to get around this problem, but here’s what I’ve found works really well instead:

Let’s say I’m conducting an exam review session and the students have had some sample problems to go over (last semester’s exam). If I start by saying “does anyone want me to go over problem 1?” or “does anyone have any questions on problem 1?” No one will say a word. I know because I’ve done this without thinking. If I say instead (or even right after) “Should I go over problem 1?” then EUREKA I’ve got half the class saying “yes!” and nodding. Something about me forfeiting control, asking the class for direction rather than their requests, gets them talking. Once students start talking they’re more likely to ask more solid questions. Even if people don’t speak, you can look around the room after asking a question like this and see them nodding or shaking their heads.

You can’t ask students to demonstrate what they don’t know.

Another, admittedly less effective means, is to invert the question. What I mean is that instead of asking “who doesn’t understand?” say “raise your hand if you understand this.” Now, this requires care because if you overuse this technique everyone will just raise their hand right away for everything. Even before that habit forms, they’ll raise their hands without really understanding. Hope is not lost though. You have to watch carefully after you say this, see how quickly their hands go up. Look for the students raising their hands slowly with their eyes wincing because they feel like they’re lying to blend in. You won’t get any questions out of this, but you can get a feel for how well the class is following and whether you need to go over something in more depth.

IMO, getting students to ask questions in a classroom is one of the most impressive skills a teacher can have. It’s really really hard.

Assigning grades the first time through

All of my grading is relative in the sense that I don’t start with a rubric. I always get through at least 50% of the exams/programs/homeworks/whatever before I get an idea of what point values certain things should be worth. If a lot of people have the same error, it might be a big mistake in my eyes, but it hints that there’s a greater misunderstanding in the class that needs addressing (unless it’s because of cheating of course).

Another problem is that I might see one submission and say “whoa this is definitely no higher than a 50,” but then later see one that’s better, but still isn’t even half-way complete. It’s really difficult to assign grades without having the class in general in mind.

Regrading, disregarding other students for the one in the room

All too often, students go to a TA to complain about their grades and are given points back when they don’t deserve a higher grade. Grading is imperfect, often a TA will accidentally deduct a point or two more than they should, it’s just the nature of the volume of things being dealt with and human error. Students will go through the points they lost with a fine-toothed comb and will be enraged easily when they feel deductions are unjust. I get it, and I have given points back (rarely) for a genuine screw-up on my part. I’m not too proud to admit I made a grading error.

Giving students points back to get them to stop complaining is a wide-spread problem. The people who suffer from this are the students who don’t complain about their grades. If half of the class complains to a crappy TA, and the TA gives them all 10+ points back, then suddenly the curve has jumped. The students who accepted responsibility for their mistakes now have lower grades and that’s not fair.

The problem is that there are other parts where I may have missed a deduction of a point or two somewhere else in the assignment or test. I’ll get students who aren’t emailing me over something obvious, but asking about a specific deduction in their work. I’ve always given students the option “if you’d like, I can regrade the whole thing, but that means the grade can go up or down, because I need to look at everything more closely.” I know others who do the same, students rarely take anyone up on this option. Grades typically go slightly lower in these cases if they change at all. It’s not a vengeful or prideful thing, but if the student wants their grade to be a 100% accurate reflection of their work, it’s gonna need a closer look. Closer looks generally reveal more mistakes.

super generic functions in c++14

Since C++14 lambdas now allow auto parameters, function objects can be pretty damn generic.

[](auto&& arg1, auto&& arg2) { return arg1 < arg2; }

Which got me thinking, what’s to stop me from defining all of my functions as lambdas. For example, this function which takes an iterable and prints it to stdout.

auto print_iterable = [](auto&& iterable) {
    for (auto&& e : iterable) {
        std::cout << e << '\n';
    }
};

int main() {
    std::vector<int> v{1,7,2,0};
    print_iterable(v);
}

This function can deduce its argument types and return type. There’s no return here of course, but there’s nothing stopping me from making this function more complex.

I haven’t thought too much about the implications of this, but it seems like I could get away without naming variable types or using templates. I’m not saying this a good idea, but it’s an interesting one.

Google style guide makes two major changes

First off, they removed the silly rule about not having copy constructors and preferring a .Clone() member function. Praise the lawd~

Objects of copyable and movable types can be passed and returned by value, which makes APIs simpler, safer, and more general.

Second, they lifted the ban on rvalue references. One may now use rvalue refs to make movable types. They’re still banning std::forward for the time being though.

Progress!

rlwrap on solaris

Trying to use sqlplus on solaris was proving to a pain do to the lack of command history. I found an oracle blog that described using rlwrap to work around this. A few other sites supported this, so I grabbed the source for rlwrap-0.41 and gave it a shot, a simple ./configure && make. When running I received an error.

% rlwrap sqlplus
Warning: rlwrap cannot determine terminal mode of sqlplus
(because: Invalid argument).
Readline mode will always be on (as if -a option was set);
passwords etc. *will* be echoed and saved in history list!

rlwrap: error: TIOCSWINSZ failed on no pty: Bad file number

After googling around I found this github issue on a repo which has since fixed the problem. The issue is in pty.c So, I cloned the source, cd’d in and did a ./configure. However this repo didn’t have a configure. Granted it works with autoreconf, I’m working on a limited system where I have very little power. Rather than descend this building-everything-from-source dive any further, I replaced the pty.c in my existing version with the pty.c from the github repo and it seems to work fine.

Hacky? yes, but I’m not planning on spending a ton of time on this system.

C++ Antipatterns: The Java constructor (and final vs const)

It seems Java (or C#) constructors are becoming increasingly pervasive in novice C++ code. A demo of what I mean:

class Point {
  public:
    Point(int a, int b) {
      this->x_ = a;
      this->y_ = b;
    }
    //...
  private:
    int x_;
    int y_;
};

This looks normal for programmers coming from a java background, but for those with C++ experience, it’s awkward that the variables x_ and y_ are initialized inside the constructor body. C++ constructors using initialization lists are more correct. They go between the ) and { of the constructor, begin with a colon, and have each part separated by a comma. They style I lean towards is as follows:

Point(int a, int b)
    : x_{a},
    y_{b}
{ }

The difference is that rather than having x_ and y_ be default constructed, then assigned, they are copy-constructed with the values of a and b. These x_ and y_ values must have been constructed because upon entering the body of the constructor, the this object must exist.

Performance

Okay, so dealing with a couple of ints, no big deal. Let’s look at a class with a string

class Person {
  public:
    Person(const std::string& n) {
      this->name_ = n;
    }
  private:
    std::string name_;
};

The semantics of this are different from what novices often think. What happens is that name_ is default constructed, and is then assigned to, and overwritten, upon entering the constructor. This means that before being assigned n, name_ already has a value! It’s the empty string! Constructing and assigning is slower than just copy constructing, so there’s a slight performance hit.

The correct constructor is, as expected

Person(const std::string& n)
    : name_{n}
{ }

Objects without default constructors

Novices don’t care too much up to this point because strings and ints both can be default constructed, and strings behave very much like POD objects. However there are many objects in C++ which don’t have default constructors at all! An example? the Person class we just made!

How about a class which has a Person in it?

class Couple {
  public:
    // fails before entering { }
    Couple(const Person& p1, const Person& p2) {
      this->person1_ = p1;
      this->person2_ = p2;
    }
  private:
    Person person1_;
    Person person2_;
};

This can’t work, because the Person class doesn’t have a default constructor. the compiler error will resemble error: no matching function for call to Person::Person(). The assignments are valid in this case, but the construction just doesn’t work. We have to use C++ style:

Couple(const Person& p1, const Person& p2)
    : person1_{p1}, // works, uses implicit copy ctor
    person2_{p2}
{ }

Beautiful!

const, and why it’s different from final

Another case to consider, in java one may have a final instance variable, signalling that once the reference has been assigned (by the constructor) it cannot be reassigned. Some incorrectly draw a parallel between const and final, they aren’t the same.

Let’s consider a Person class where we also have a string for their social security number. This number can’t change, so we want it to be const. The java-style constructor fails here.

class Person {
  public:
   Person(const std::string& n, const std::string& s) {
     this->name_ = n;
     this->ssn_ = s; //fails, can't assign to const var
   }
  private:
   std::string name_;
   const std::string ssn_;
};

Remember, the semantics differ, this code default constructs ssn_ and then assigns to it. However, we can’t assign to const variables, we can only construct them (or if you prefer, initialize them). Instead we must use the correct C++ constructor style.

Person(const std::string& n, const std::string& s)
    : name_{n},
    ssn_{s} // works, construction not assignment!
{ }

Since C++ novices often don’t care about constness, it’s hard to convince them to take this seriously last one seriously.

Other cases

There are other types of objects that cause problems. One is the class of objects without an assignment operator (such as the Person with a const ssn), objects where default construction followed by assignment does get expensive (std::array for example), objects that can be moved but not copied need to be handled differently, etc.

This is not an academic exercise, the problems with java-style constructors are very real.