Is there a way to track the output of a compiler template argument?

I am looking for a way to get some trace log from the compiler logic when it tries to infer the types of the template arguments whenever it succeeds or not. So, for example, this code:

#include <iostream> #include <vector> #include <type_traits> template<typename T> decltype(auto) foo(T&& t) -> decltype(t + t) { return t + t; } template<typename T> decltype(auto) foo(T&& t) -> decltype(t.size()) { return t.size(); } int main() { std::cout << foo(10) << '\n' << foo(std::vector<int>{1,2,3}) << '\n'; } 

I would like to get something like:

 foo(10) candidate: decltype(auto) foo(T&& t) -> decltype(t * t): seems valid candidate: decltype(auto) foo(T&& t) -> decltype(t.size()): wrong one 

Compilers are pretty good at this, for example, with the provision of ambiguous calls. For example. if I called foo(std::string("qwe")); I would get:

 main.cpp: In function 'int main()': main.cpp:23:31: error: call of overloaded 'foo(std::__cxx11::basic_string<char>)' is ambiguous foo(std::string("qwe")); ^ main.cpp:7:20: note: candidate: decltype ((t + t)) foo(T&&) [with T = std::__cxx11::basic_string<char>; decltype ((t + t)) = std::__cxx11::basic_string<char>] decltype(auto) foo(T&& t) -> decltype(t + t) ^~~ main.cpp:13:20: note: candidate: decltype (t.size()) foo(T&&) [with T = std::__cxx11::basic_string<char>; decltype (t.size()) = long unsigned int] decltype(auto) foo(T&& t) -> decltype(t.size()) ^~~ 

And if this explicit feedback is not possible, maybe there is a way to get an idea of ​​the "semi-compiled" code, with the template output already done?

Does any of the compilers have such a function? gcc, clang, mvsc?

Backround:

This example is pretty simple and obvious, but I'm experimenting with the ranges::v3 library and trying to understand why one particular case works and another doesn't. (Technically, iterator_range<Handmade InputIterator> with view::take(3) returns void instead of some fancy range , but it doesn't matter for this question. I want to track the output on almost the same line, but with iterator_range<ContiguousIterator> and see the difference.

+8
c ++ c ++ 14 c ++ 17
source share
1 answer

Templight is a clang-based tool for tracking the progress of templates. It has an interface similar to the gdb debugger, so you can focus on a specific instance that causes errors or does not behave as expected. Or you can force it to display the β€œprofile” of the template instances, which will give an overview of all of them.

There is also a Templar tool associated with it, which seems to be a graphical interface for the Templight debugging mode, although I have not tried it myself.

+6
source share

All Articles