isSet()或operator void *()或显式操作符bool()或别的? [英] isSet() or operator void*() or explicit opertor bool() or something else?
问题描述
什么是最先进的功能,检查值是否已设置?
例如,下面的迭代器解析单元格。
一些单元格包含值,其他单元格为空。
For example, the below iterator parses cells. Some cells contain a value, other cells are empty.
最方便的方法是什么?
$ b
What is the most convenient way?
struct iterator
{ //usage:
bool isset() const // if (it.isset())
bool isSet() const // if (it.isSet())
bool empty() const // if (it.empty())
bool is_set() const // if (it.is_set())
bool is_valid() const // if (it.is_valid())
operator void*() const; // if (it)
explicit operator bool() const; // if ((bool)it) or if(it) //thanks @stijn
operator bool() const; // if (it) //why not implicit conversion?
bool operator!() const; // if (!!it)
//throwing exception as pointed out by @MatthieuM
Type get() { if (isSet()) return value_; else throw; }
//usage:
// try { // if (it.isSet()) {
// Type x = it.get(); // Type x = it.get();
// } // }
// catch (...) { // else {
// //empty // //empty
// } // }
//please feel free to propose something different
...
};
反映:
- 我的老板不明白
isset()
=>已重命名为isSet()
-
empty()
更多关于容器收集,而不只是一个单一的单元格:( -
operator void *
似乎是合乎逻辑的方式,但已弃用在C ++ 11流 -
显式运算符
是尚未支持(我的代码必须符合旧的编译器)
- my boss does not understand
isset()
=> renamed toisSet()
empty()
is more about container collection, not just one single cell :(operator void*
seems to be the logical way but deprecated in C++11 streamsexplicit operator
is not yet supported (my code have to be compliant with old compilers)
$ b b
我正在阅读:
I am reading:
- http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Safe_bool
- ( http://www.artima.com/cppsource/safebool.html )
- C ++ 11中的安全bool成语已过时?
- 安全bool习语和显式运算符bool之间的不兼容性
- 安全bool成语在boost?
- 提升源代码...
- http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Safe_bool
- ( http://www.artima.com/cppsource/safebool.html )
- Is the safe-bool idiom obsolete in C++11?
- Incompatibilities between safe bool idiom and explicit operator bool
- Safe bool idiom in boost?
- boost source code...
推荐答案
我印象深刻的是 explicit_cast< T>
=http://rads.stackoverflow.com/amzn/click/0321228774 =nofollow>不完全的C ++:实用解决方案[...] 尚未提及。这个概念很简单 - 你设置一个伪关键字,它实际上是一个模板类,实现你想要的转换。我一直在使用此自己的C ++后端库,没有任何重要问题。 p>
I'm impressed that explicit_cast<T>
from Imperfect C++: Practical Solutions [...] hasn't been mentioned. The concept is very simple - you set up a pseudo-keyword that actually is a templated class implementing the conversion that you want. I've been using this in my own C++ backports library without any important issue.
class MyClass {
...implementation
operator explicit_cast<bool> () const {
(return a bool somehow)
}
};
您使用伪关键字,就像您预期的那样:
You use the pseudo-keyword just as you would expect it to work:
MyClass value;
...
if ( explicit_cast<bool>(myobject) ) {
do something
} else {
do something else
}
...
最好的部分是,这个解决方案可以完美映射到原生显式运算符
在C ++ 11中,导致基本上零开销和本地语法。因此,它也更通用,比试图找出是否调用isset,is_set,is_Set,isSet等等...
The best part of all, this solution can be mapped perfectly to native explicit operator
in C++11, resulting in essentially zero overhead and native syntax. As a result, it's also more generic than trying to figure out if to call "isset", "is_set", "is_Set", "isSet", etc...
这篇关于isSet()或operator void *()或显式操作符bool()或别的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!