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.
c ++ c ++ 14 c ++ 17
R2RT
source share