Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

c++ - Automatically count the number of instantiated classes in a TMP?

Given a template metaprogram (TMP), do C++ compilers produce build statistics that count the number of instantiated classes? Or is there any other way to automatically get this number? So for e.g. the obiquitous factorial

#include <iostream>

template<int N> struct fact { enum { value = N * fact<N-1>::value }; };
template<> struct fact<1> { enum { value = 1 }; };

int main()
{
    const int x = fact<3>::value;
    std::cout << x << "
";
    return 0;
}

I would like to get back the number 3 (since fact<3>, fact<2>, and fact<1> are instantiated). This example if of course trivial, but whenever you start using e.g. Boost.MPL, compile times really explode, and I'd like to know how much of that is due to hidden class instantiations. My question is primarily for Visual C++, but answers for gcc would also be appreciated.

EDIT: my current very brittle approach for Visual C++ is adding the compile switch from one of Stephan T. Lavavej's videos /d1reportAllClassLayout and doing a grep + word count on the output file, but it (a) increases compile times enormously and (b) the regex is hard to get 100% correct.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I made a one-line change to GCC that makes it print out the name of each class template as it instantiates it. You can already call the C++ front-end cc1plus directly without the -quiet flag to get the same for function templates.

I haven't got round to turning that into a proper GCC option yet, it's just a hack on my own source tree. I'm thinking of implementing it as a plugin instead, but it's not near the top of my TODO list.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...