是否有任何方法可以避免警告/错误模板实例化回溯
Is there anyway to avoid warning/error template instantiation backtrace?
花了一些时间,完全不知道这是否可能。所以我想我会在这里问。那么,在gcc/clang上显示警告/错误时,有没有什么聪明的方法可以强制不打印模板回溯?
示例:
template<int I = 0, typename = void>
struct warn {
unsigned : I;
};
struct hello_world {};
template<int I>
class a : warn<I, hello_world> {};
template<int I>
class b : a<I>{};
template<int I>
class c : b<I> {};
template<int I>
class d : c<I> {};
int main() {
d<80>{};
}
给出:
test.cpp:3:5: warning: size of anonymous bit-field (80 bits) exceeds size of its type; value will be truncated to 32 bits
unsigned : I;
^
test.cpp:8:11: note: in instantiation of template class 'warn<80, hello_world>' requested here
class a : warn<I, hello_world> {};
^
test.cpp:11:11: note: in instantiation of template class 'a<80>' requested here
class b : a<I>{};
^
test.cpp:14:11: note: in instantiation of template class 'b<80>' requested here
class c : b<I> {};
^
test.cpp:17:11: note: in instantiation of template class 'c<80>' requested here
class d : c<I> {};
^
test.cpp:20:2: note: in instantiation of template class 'd<80>' requested here
d<80>{};
^
1 warning generated.
因此,预期结果将是例如:
test.cpp:3:5: warning: size of anonymous bit-field (80 bits) exceeds size of its type; value will be truncated to 32 bits
unsigned : I;
^
test.cpp:8:11: note: in instantiation of template class 'warn<80, hello_world>' requested here
class a : warn<I, hello_world> {};
有-ftemplatebacktracelimit=1-ferrorlimit=1,但我想知道是否有可能从源代码中做到这一点。
为什么我需要这样的功能?好吧,我通过enable-if使用概念模拟,但不幸的是,我的概念构造中有模板转换运算符,不能只返回值和静态断言或enable-if,因为信息不再可用。因此,我认为也许警告+概念可以做到这一点。假设我仍然有这个概念,并用有用的东西打印一行警告,然后像往常一样用enable-if禁用该功能。
您可以使用别名而不是类/结构来接近:
template<int I = 0, typename = void>
struct warn {
unsigned : I;
};
struct hello_world {};
template<int I>
using a = warn<I, hello_world>;
template<int I>
using b = a<I>;
template<int I>
using c = b<I>;
template<int I>
using d = c<I>;
int main() {
(void)d<80>{};
}
输出:
test.cpp:3:5: warning: size of anonymous bit-field (80 bits) exceeds size of its type; value will be truncated to 32 bits
unsigned : I;
^
test.cpp:20:9: note: in instantiation of template class 'warn<80, hello_world>' requested here
(void)d<80>{};
^
1 warning generated.
相关文章:
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 错误的cv::face FacemarkLBF实例化
- 无法使用 SWIG 在 Python 中实例化C++类(获取属性错误)
- gcc出现不明确的模板实例化错误
- 尝试实例化模板类的对象时出现"No Matching Constructor"错误
- 无法实例化抽象类错误
- 错误 C2280 / 在 std::vector 中实例化对象时复制构造函数出现问题?
- 模板实例化分析错误
- unique_ptr 使用尚未定义的参数进行实例化不会导致错误
- 与模板实例化相关的编译错误
- 从模板实例化/类型推断中查找错误消息的实际来源
- 类模板实例化错误
- 错误 C2908:显式专用化; 已被实例化
- C++11 列出 push_back() 实例化错误
- 如何修复错误C2259:无法实例化抽象类
- 使用非量表类型实例化模板时修复编译错误
- 由于命名空间而导致的对象实例化错误
- 无法在好友函数中实例化类?我没有得到在范围错误中声明
- 实例化对象时如何修复分段故障错误
- 将 SFINAE 上下文中不正确的模板实例化的硬错误转变为软错误