以下代码如何工作?
typedef char (&yes)[1];
typedef char (&no)[2];
template <typename B, typename D>
struct Host
{
operator B*() const;
operator D*();
};
template <typename B, typename D>
struct is_base_of
{
template <typename T>
static yes check(D*, T);
static no check(B*, int);
static const bool value = sizeof(check(Host<B,D>(), int())) == sizeof(yes);
};
//Test sample
class Base {};
class Derived : private Base {};
//Expression is true.
int test[is_base_of<Base,Derived>::value && !is_base_of<Derived,Base>::value];
Run Code Online (Sandbox Code Playgroud)
请注意,这B是私人基地.这是如何运作的?
注意operator B*()是const.它为什么如此重要?
为什么template<typename T> static yes …
请考虑以下代码:
void ListenerImpl::attach(boost::shared_ptr<ISubscriber> subscriber)
{
boost::unique_lock<boost::mutex>(mtx);
subscribers.push_back(subscriber);
}
void ListenerImpl::notify(MsgPtr msg)
{
boost::unique_lock<boost::mutex>(mtx);
//notify all subscribers
BOOST_FOREACH(boost::shared_ptr<ISubscriber> subscriber, subscribers){
subscriber->update(msg);
}
}
Run Code Online (Sandbox Code Playgroud)
(这是GoF中描述的观察者模式的实现.)这里的用户干预是保护attach()和notify()不同时运行,因此boost :: unique_lock.目标是保护subscribers容器.
但确实很难注意到锁实际上只是暂时的(仔细看,没有为它们指定名称).因此,当临时被破坏时,互斥锁上的锁定将被立即释放,即代码不是线程安全的.我希望在这种情况下编译器警告.像"未使用的临时"之类的东西.
更糟糕的是,cppcheck也不会认识到这个错误.(cppcheck:ac/c ++代码分析工具http://sourceforge.net/apps/mediawiki/cppcheck/index.php?title=Main_Page)
Gcc对未使用的变量发出警告.这里的临时变量是一个未使用的变量,绝对是程序员注意力不集中的结果.那么,为什么在这种情况下没有警告呢?发现这种情况可能太复杂了吗?