C++ まちがいさがし

デバッグ用に、初期化リストの中で出力を行える次のクラスを考える:

// cdebug.hpp
#include <iostream>

class CDebug
{
protected:
    int N;

public:
    CDebug(int val)
        : N(val) { std::cout << val << std::endl; }
    void print() { std::cout << N << std::endl; }
};

使ってみる。
テンプレート引数の値を表示するプログラム:

// main.cpp
#include "cdebug.hpp"

template <int N>
class Test : public CDebug
{
public:
    Test() : CDebug(N) {}
};

class Run
{
private:
    Test<10> test;
public:
    Run() : test() {}
};

int main(int argc, char *argv[])
{
    Run run;
    return 0;
}

実行結果

$ g++ main.cpp
$ ./a.out 
-1218330636

!!??

これに半日消費させられた... orz