假设我正在维护一个接受两个参数(两个指针)的库函数。第二个参数仅是为了向后兼容而存在。调用者应始终传递NULL。我想在我的头文件中放入一些内容,如果第二个参数不是编译时常量NULL,则会使编译器发出警告。我以为我可以使用GCC __builtin_constant_p和__attribute__((warning))扩展名来做到这一点:
extern void thefun_called_with_nonnull_arg (void)
__attribute__((__warning__(
"'thefun' called with second argument not NULL")));
extern int real_thefun (void *, void *);
static inline int
thefun (void *a, void *b)
{
if (!__builtin_constant_p(b) || b != 0)
thefun_called_with_nonnull_arg();
return real_thefun(a, b);
}
int warning_expected (void *a, void *b)
{
return thefun(a, b);
}
int warning_not_expected (void *a)
{
return thefun(a, 0);
}
Run Code Online (Sandbox Code Playgroud)
但这不适用于我测试过的任何版本的GCC。两次致电我都会收到警告thefun。(编译器资源管理器演示。)
谁能建议一种替代构造,它会为warning_expected而不是发出警告warning_not_expected?
笔记:
有什么方法可以防止或不鼓励在仅以Qt5编写的项目中使用来自Qt4 的旧Signal-Slot语法?
在我们当前的项目中,没有出现旧语法,我也没有任何理由支持它们。因此,我们想完全禁用它以防止意外使用。这是可能的,例如通过在中定义某些符号。pro文件?
我知道使用自定义Linter规则应该可以实现,但不幸的是我们还没有集中化规则。
//old way. should throw a compiler error or warning
connect(sender, SIGNAL(sig), receiver, SLOT(slt));
//new way
connect(sender, &Send::sig, receiver, &Rec::slt);
Run Code Online (Sandbox Code Playgroud) 我有很多模板代码.由于坏的模板代码不会抛出编译器错误,除非它被编译,有什么方法可以检查编译器实际上"编译"哪些模板函数,哪些被完全忽略?
编辑2:
如果特定的类模板或函数模板被实例化一次,对于任何参数类型,那就没问题.我想要从未以任何形式实例化的函数/类模板列表.
一个特定的例子如下.它们是两个不同的模板函数,我想知道其中一个或两个是否从未实例化.
template <typename T_InputItr, typename T_Distance>
void advance( T_InputItr& aItr, T_Distance aN, bidirectional_iterator_tag )
template <typename T_InputItr, typename T_Distance>
void advance( T_InputItr& aItr, T_Distance aN, random_access_iterator_tag )
Run Code Online (Sandbox Code Playgroud)
编辑:目前,对于类,我在.cpp文件中手动实例化它们,如下所示:
template TClass<int>;
Run Code Online (Sandbox Code Playgroud)
对于我感兴趣的所有类型.这很好,很好.但那就是我记得那样做的.有时我需要编写很多小模板类/函数,我忘记手动实例化一些函数/类模板,然后在后面找出.我想编译器告诉我.
或者,如果我可以获得实例化的函数/类模板列表(对于任何参数),那么我可以将它与我可能在代码中grep的完整列表进行比较.
另一个好处是"测试"在模板类中编译哪些方法,该模板类使用类型特征来有选择地编译出某些函数.在继续之前,我想确定我选择正确功能的逻辑是否正确.
当对编译时 constexpr 的测试评估为真时,是否可以打印消息?像 static_assert 这样的东西而不停止编译器。
constexpr Application::Mode SETUP_MODE = Application::Debug;
// Can I somehow test if the SETUP_MODE is debug for pragma message?
#pragma message("Application mode is set to Debug!")
static_assert(SETUP_MODE != Application::Debug,
"Application mode is set to Debug!"); // Can I somehow just print this message without aborting the compilation?
Run Code Online (Sandbox Code Playgroud) 我正在学习如何使用std::enable_if,到目前为止,我在有条件地启用和禁用课程中的方法方面取得了一定程度的成功。我针对布尔值对方法进行模板化,并且此类方法的返回类型是std::enable_if此类布尔值。这里的最小工作示例:
#include <array>
#include <iostream>
#include <type_traits>
struct input {};
struct output {};
template <class io> struct is_input { static constexpr bool value = false; };
template <> struct is_input<input> { static constexpr bool value = true; };
template <class float_t, class io, size_t n> class Base {
private:
std::array<float_t, n> x_{};
public:
Base() = default;
Base(std::array<float_t, n> x) : x_(std::move(x)) {}
template <class... T> Base(T... list) : x_{static_cast<float_t>(list)...} {}
// Disable the getter if it …Run Code Online (Sandbox Code Playgroud) 可能重复:
是否存在static_warning?
我经常使用#warning通过我的代码分散来标记我需要回到的地方.
喜欢:
#warning The blahblah hasn't been implemented yet; use foo to do so.
Run Code Online (Sandbox Code Playgroud)
我想创建一个宏,可选地不显示与我的笔记特别相关的警告,但显示来自编译器和其他库的实际警告.
就像是:
#ifdef SUPPRESS_NOTES
#define BUILD_NOTE
#else
#define BUILD_NOTE #warning Note: msg
#endif
Run Code Online (Sandbox Code Playgroud)
不幸的是,#warning首先得到评估.有什么方法可以做到这一点吗?我使用GCC(MinGW).