咋一看你能看明白这是什么吗?来自<boost/signals/signal_template.hpp>
1 template< 2 typename R, 3 BOOST_SIGNALS_TEMPLATE_PARMS 4 BOOST_SIGNALS_COMMA_IF_NONZERO_ARGS 5 typename Combiner, 6 typename Group, 7 typename GroupCompare, 8 typename SlotFunction 9 > 10 typename BOOST_SIGNALS_SIGNAL< 11 R, BOOST_SIGNALS_TEMPLATE_ARGS 12 BOOST_SIGNALS_COMMA_IF_NONZERO_ARGS 13 Combiner, Group, GroupCompare, SlotFunction>::result_type 14 BOOST_SIGNALS_SIGNAL< 15 R, BOOST_SIGNALS_TEMPLATE_ARGS 16 BOOST_SIGNALS_COMMA_IF_NONZERO_ARGS 17 Combiner, Group, GroupCompare, SlotFunction 18 >::operator()(BOOST_SIGNALS_PARMS) 19 { 20 // Notify the slot handling code that we are making a call 21 BOOST_SIGNALS_NAMESPACE::detail::call_notification notification(this->impl); 22 23 // Construct a function object that will call the underlying slots 24 // with the given arguments. 25 #if BOOST_SIGNALS_NUM_ARGS == 0 26 BOOST_SIGNALS_ARGS_STRUCT_INST args; 27 #else 28 BOOST_SIGNALS_ARGS_STRUCT_INST args(BOOST_SIGNALS_ARGS); 29 #endif // BOOST_SIGNALS_NUM_ARGS > 0 30 call_bound_slot f(&args); 31 32 typedef typename call_bound_slot::result_type call_result_type; 33 optional<call_result_type> cache; 34 // Let the combiner call the slots via a pair of input iterators 35 return combiner()(slot_call_iterator(notification.impl->slots_.begin(), 36 impl->slots_.end(), f, cache), 37 slot_call_iterator(notification.impl->slots_.end(), 38 impl->slots_.end(), f, cache)); 39 }
尤其后面35行的“return combiner()(slot_call_iterator(notification.impl->slots_.begin(), 36 impl->slots_.end(), f, cache), 37 slot_call_iterator(notification.impl->slots_.end(), 38 impl->slots_.end(), f, cache));”
我也是被这句话困扰了半个小时,后来追踪源码和自己理解终于明白了。
请看下面的简单模拟
1 #include <boost/signals.hpp> 2 #include <iostream> 3 4 int testadd(int a, int b) 5 { 6 return a+b; 7 } 8 typedef int(*pfun)(int, int); 9 pfun testfun() 10 { 11 return &testadd; 12 } 13 class testclass 14 { 15 private: 16 int a; 17 int b; 18 19 public: 20 testclass() {} 21 testclass(int x, int y) : a(x), b(y) {} 22 int operator()() 23 { 24 return testfun()(a, b); 25 } 26 }; 27 28 int _tmain(int argc, _TCHAR* argv[]) 29 { 30 testclass obj(3, 4); 31 std::cout << obj() << std::endl; 32 33 return 0; 34 }
通过上面的代码你能看明白了吧,恩C++的语法方面就是比较复杂,一不留神就发现可能“很陌生”。
上面的问题的产生主要来自我对boost::Signals的学习
下面是书上的示例:
1 #include <boost/signals.hpp> 2 #include <iostream> 3 4 void my_first_slot() 5 { 6 std::cout << "void my_first_slot() "; 7 } 8 class my_second_slot 9 { 10 public: 11 void operator()() const { std::cout << "void my_second_slot::operator()() const "; } 12 }; 13 14 int _tmain(int argc, _TCHAR* argv[]) 15 { 16 boost::signal<void ()> sig; 17 sig.connect(&my_first_slot); 18 sig.connect(my_second_slot()); 19 std::cout << "Emitting a signal... "; 20 sig(); 21 22 return 0; 23 }
20行的sig()引发的思考。