大佬教程收集整理的这篇文章主要介绍了C语言模板问题,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
template<typename T> void unused(T const &) { /* Do nothing. */ } int main() { volatile bool x = false; unused(!X); // type of "!x" is bool }
如下所示,g v3.4.6编译器抱怨:
test.cc: In constructor `test::test()': test.cc:11: error: invalid initialization of reference of type 'const volatile bool&' from expression of type 'volatile bool' test.cc:3: error: in passing argument 1 of `void unused(const T&) [with T = volatile bool]'
这里的目标是在优化的代码中使用未使用的抑制未使用的变量警告.我有一个执行断言检查的宏,在优化代码中断言消失了,但是我希望断言表达式中的任何变量都保持引用,这样我才能在优化代码中得到未使用的变量警告.在unused()模板函数的定义中,我使用引用,以便不会无意中运行复制构造函数代码,以便编译器可以完全忽略对未使用的调用.
对于那些感兴趣的人,断言宏看起来像这样:
#ifdef NDEBUG # define Assert(expression) unused(expression) #else // not NDEBUG # define Assert(expression) \ { \ bool test = (expression); \ \ if (!test) { \ if (StopHere(__LINE__,__FILE__,__PRETTY_FUNCTION__,\ #expression,falsE)) { \ throw Exit(-1); /* So that destructors are run. */ \ } \ } \ } #endif // else not NDEBUG
对于上面的测试用例,我可以通过添加另一个类似的未使用函数来消除错误:
template<typename T> void unused(T const) { /* Do nothing. */ }
然而,当参数可以被引用时,其他调用unused()的情况由于模糊而失败,例如:
file.h:176: error: call of overloaded `unused(bool)' is ambiguous myAssert.h:27: note: candidates are: void unused(T) [with T = bool] myAssert.h:34: note: void unused(const T&) [with T = bool]
所以我的问题是,如何更改unused()或重载它以满足以下要求:
>对unused()的调用可以由编译器优化为无操作.
>它会导致传递给unused()的表达式中存在的任何变量出现使用,因此不会导致关于定义但未使用的警告.
> unused()的参数可能会或可能不会被引用.
> unused()的参数可能是具有昂贵的复制构造函数的对象,在调用unused()时不应调用该对象.
谢谢.
-威廉
unused( bool( !readWriteActivated) ); // add bool() to any (!volatile_bool_var)
旧答案(但仍然不是一个坏主意)
如果我回想起const-volatile资格规则,你只需要更多地验证虚拟变量.基本上,您只想在声明的类型中将错误消息反馈:vP.
template<typename T> void unused(T const volatile &) { // only change is to add "volatile" /* Do nothing. */ }
另外,很好的是你把const放在它所属的类型之后.
以上是大佬教程为你收集整理的C语言模板问题全部内容,希望文章能够帮你解决C语言模板问题所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。