我正在尝试在编译时确定函数是否被调用。具体来说,如果该函数被调用,我想抛出一个静态断言失败:
我希望这样做的原因是,如果使用错误的条件调用
我不想在
这个能做到吗?
template <typename T>
auto Function(T value) -> std::enable_if<someCondition, int>
{
// this is the function I want to call
}
template <typename... T>
int Function(T...)
{
// This function should never be called, instead I want
// a compile-time failure if this is called, because it
// means the above function wasn't successfully resolved.
}
我希望这样做的原因是,如果使用错误的条件调用
Function()
,将导致成千上万行的编译器错误消息,对于不熟悉代码库的人来说,这些消息并没有什么帮助。我不想在
Function
中放置static_assert
的原因是因为我们有很多这样的函数,并且我们有手段通过宏生成Catch-all
版本,这可以避免代码库不必要的增长,同时产生更有用的错误消息。这个能做到吗?
static_assert(false,...)
,它就会失败,对吧? - quant