在不依赖自动模板推导的情况下检查模板方法是否存在

Check if template method exists without relying on automated template deduction

如果定义了具有给定签名的模板方法,是否可以编写测试程序?

方法签名如下:

template<typename ReturnType>
ReturnType get(std::string) { return std::declval<ReturnType>(); }

我在这里搜索,但我发现的都是依赖于自动模板推导的机制 (例如 How to test if template function exists at compile time

#include <type_traits>
#include <string>

template <typename T>
constexpr bool test() { return is_invocable<decltype(get<T>), std::string>::value; }

参见std::is_invocable。但是,如果 get<T> 不编译,test<T> 仍然不编译;请参阅下面的评论。