Issue
How to print automatically deduced template arguments at compile time?
std::pair x(1, 2.0);
In the above example the type of x is std::pair<int, double>. Is there a way to print the deduced type of x at compile time, e.g. as std::pair<int, double> or in some other readable form?
Edit. I am using a library (DPC++) that relies on C++ 17 automatic template argument deduction. Sometimes it is difficult to guess how template argument were deduces.
Solution
Here's one (admittedly roundabout and verbose) approach:
#include <utility>
template <class T>
struct type_reader {
type_reader() {
int x{0};
if (x = 1) {} // trigger deliberate warning
}
};
int main() {
std::pair x(1, 2.0);
type_reader<decltype(x)> t;
}
Depending on your compiler and settings, you can get a warning like
<source>:7:19: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
if (x = 1) {} // trigger deliberate warning
<source>:13:34: note: in instantiation of member function 'type_reader<std::pair<int, double>>::type_reader' requested here
type_reader<decltype(x)> t;
and you can see the type std::pair<int, double>>
in that mess if you squint.
Answered By - Ayjay