模板实例化上的SFINAE错误
SFINAE on template instantiation error?
我问的可能是不可能的,但我会问只是为了确定。正如前面在这个问题中所问和回答的:SFINAE和decltype(auto),具有自动返回类型的函数可能不是SFINAE,因为返回类型的推导会导致模板实例化。在实例化过程中,编译器将产生一个错误。
然而,也许有人知道一些疯狂的技巧,将一段代码中的实例化错误转化为另一段代码的替换失败,从而引发SFINAE?
以下是一段导致错误的代码示例:
#include <utility>
struct no_call_ops { };
struct caller
{
// Goal is to detect whether a particular Handler type
// will be ok to pass into caller
template <typename Handler>
auto operator() (Handler&& handler, int v)
{
handler(3.14);
while (v--)
{
handler("hello world");
}
// etc...
// more calls to handler that I don't want to
// express in the trailing return type.
}
};
struct fallback { };
struct tester
{
// Would be nice if this fails substitution...
template <typename Caller, typename Handler, typename... Args>
auto operator() (Caller&& caller, Handler&& handler, Args&&... args)
-> decltype(caller(handler, std::forward<Args>(args)...))
{ }
fallback operator() (...) { return fallback{}; }
};
// want detected_t to be "fallback"
using detected_t = decltype(tester{}(caller{}, no_call_ops{}, 42));
在Godbolt上玩代码。
谢谢!
这种尾随返回类型可以接受吗?
#include <utility>
#include <functional>
using namespace std;
struct no_call_ops { };
template<class T>
using int_t = int;
struct caller
{
template <typename Handler, int_t<typename result_of<Handler>::type> = 0>
auto operator() (Handler&& handler, int) -> typename result_of<Handler>::type
{
handler(3.14);
}
};
struct fallback { };
struct tester
{
template <typename Caller, typename Handler, typename... Args>
auto operator() (Caller&& caller, Handler&& handler, Args&&... args) -> decltype(caller(handler, std::forward<Args>(args)...))
{ }
fallback operator() (...) { return fallback{}; }
};
using detected_t = decltype(tester{}(caller{}, no_call_ops{}, 42));
这对你有用吗?
#include <utility>
struct no_call_ops { };
struct caller
{
template <typename Handler, class = decltype( std::declval<Handler>()(3.14) )>
decltype(auto) operator() (Handler&& handler, int)
{
handler(3.14);
}
};
struct fallback { };
struct tester
{
template <typename Caller, typename Handler, typename... Args>
auto operator() (Caller&& caller, Handler&& handler, Args&&... args) -> decltype(caller(handler, std::forward<Args>(args)...))
{ }
fallback operator() (...) { return fallback{}; }
};
using detected_t = decltype(tester{}(caller{}, no_call_ops{}, 42));
相关文章:
- VS2017 上 SFINAE 的编译错误
- 进行 SFINAE 练习时编译错误
- 尝试编译SFINAE检查中使用的方法体时发生编译错误
- C++ 带有 decltype 的 SFINAE:替换失败成为错误?
- 使用模板时可能是编译器中的 SFINAE 错误?
- 尝试使用具有尾随返回类型的 lambda 进行 SFINAE 时出现硬错误
- 如何对 SFINAE 进行"deep",即当替换导致代码中进一步出现一些编译错误时?
- 尾随返回类型中带有 SFINAE 的 GCC 错误
- 防止编码器错误 - 忘记在"std::enable_if<>::type"中添加"::type" (SFINAE)
- 为什么这个 SFINAE 在 gcc 中给出错误
- 在模板 SFINAE 约束中使用间接寻址级别会导致硬错误
- 使用C ,使用SFINAE测试静态成员的存在,返回错误的值
- 将 SFINAE 上下文中不正确的模板实例化的硬错误转变为软错误
- 在VS2015编译中键入Sfinae,但会产生错误
- VC++ SFINAE 给出错误 C2070:"重载函数":操作数大小非法
- 从MSVC2015更新2移植到GCC 5.3-SFINAE错误
- C++模板sfinae错误
- 模板实例化上的SFINAE错误
- 使用 SFINAE 但不创建编译错误
- VS2013 上的 SFINAE 错误