禁止使用“static_assert”函数
Forbids functions with `static_assert`
我想阻止某些函数被调用。让我们忽略通过函数指针或其他方式调用函数的情况,只关注直接函数调用的情况。我可以用= delete
做到这一点。然而,发布的诊断信息并不丰富。我考虑过使用static_assert
,使用它可以提供自定义诊断消息。我在函数体中放置了一个static_assert(false, ...)
语句,希望它在调用函数时激发。然而,事实证明,即使没有调用函数,static_assert
也会失败。有什么建议吗?
附加说明:无条件禁止此功能。因此,std::enable_if
在此不适用。创建这样一个函数的动机是,我想防止某些使用,否则会以过载解决方案编译得很好。所以我不能只是删除函数。deprecated
不是我想要的。我想要一个编译错误,而不是警告。
我同意其他人的观点,即根本不应该使用static_assert
,而是将该函数标记为不推荐使用。
CCD_ 8离子在编译时激发。对于普通函数,这是解析它的时间,而不是调用它的时间。然而,对于template
,它是实例化的时间。所以你可以把你的函数做成这样的template
。
template <typename...>
struct always_false { static constexpr bool value = false; };
template <typename... Ts>
void
never_call_me(Ts&&...)
{
static_assert(always_false<Ts...>::value,
"You should have never called this function!");
}
如果typename...
不适合您(因为函数重载),请尝试将其缩小到只匹配您想要出错的内容。
这里使用的技巧是always_false<Ts...>::value
取决于类型参数Ts...
,因此在template
被实例化之前不能对其进行评估。(尽管我们可以清楚地看到它将永远是false
。)
如果它是成员函数,那么= delete
是最好的(最可移植的)选择。否则,GCC和MSVC都支持将函数标记为"不推荐使用",这将导致编译器在调用函数时发出警告。
从C++标记为弃用:
#ifdef __GNUC__
#define DEPRECATED(func) func __attribute__ ((deprecated))
#elif defined(_MSC_VER)
#define DEPRECATED(func) __declspec(deprecated) func
#else
#pragma message("WARNING: You need to implement DEPRECATED for this compiler")
#define DEPRECATED(func) func
#endif
用法:
DEPRECATED(void badidea(int a, const char* b));
现在使用C++14,我们可以将其写成:
#define DEPRECATED(func, reason) [[deprecated(reason)]] func
使用:
DEPRECATED( void badidea(int a, const char* b), "This function was a bad idea");
作为@5gon12eder的好答案的较短版本,您可以简单地使用
template<typename ... Ts>
void never_call_me(Ts&&...ts)
{
static_assert(not (std::is_same_v<Ts,Ts> && ...),
"You should have never called this function!");
}
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- 我关于函数"Assert"的C++代码有问题
- assert() 在发布模式下充当标识函数吗?
- 如何编写一个接受如下断言消息的自定义断言函数:assert(false) << "assertio
- 能够使用debug.assert()函数需要什么
- 如何使用assert函数
- assert和静态assert函数的使用
- assert()函数未在反汇编中显示
- 在 Xcode [c++] 中在断言 (assert.h) 中定义 lambda 时,为类似函数的宏调用提供过多参数会导
- C++中的函数assert()崩溃
- Assert()函数抛出一个错误——操作符有问题
- 如何避免CRT对话框与assert(.)函数
- assert函数如何在失败时返回文件名和代码行
- 无法解析函数"assert"