我正在寻找方法从编译器的逻辑中获取一些跟踪日志,当它试图推断模板参数类型时,无论何时成功.例如,给出代码:
#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';
}
Run Code Online (Sandbox Code Playgroud)
我很想得到类似的东西:
foo(10)
candidate: decltype(auto) foo(T&& t) -> decltype(t * t): seems valid
candidate: decltype(auto) foo(T&& t) -> decltype(t.size()): wrong one
Run Code Online (Sandbox Code Playgroud)
编译器已经非常擅长,例如提供不明确的调用.例如,如果我打电话给foo(std::string("qwe"));我:
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())
^~~
Run Code Online (Sandbox Code Playgroud)
而且,如果这种显式反馈不可能,也许有办法获得"半编译"代码的视图,所有模板推导已经完成了吗?
有没有编译器有这样的功能?gcc,clang,mvsc?
这个例子非常简单ranges::v3明了,但我确实试验了图书馆并且很难理解为什么一个特定的案例能够起作用以及为什么另一个案例不起作用.(从技术上来说,iterator_range<Handmade InputIterator>用view::take(3)回归void而不是某些花哨的东西range,但这不是问题的问题.我想在几乎相同的线上跟踪演绎,但有iterator_range<ContiguousIterator>并看到差异.