संकलित करने में विफल रहता है मैं C++ concurrency in action पढ़ रहा हूं। अध्याय 2.4 एक समानांतर_अंकुलेट एल्गोरिदम का वर्णन करता है।std :: रेफ तर्क के साथ धागा लेम्बा लेना
मैंने एक सीखने के प्रयोग के रूप में - एक सामान्य लैम्ब्डा के साथ उपयोग किए गए मज़ेदार को प्रतिस्थापित करने की कोशिश की।
#include <thread>
template <typename T>
struct f {
void operator() (T& result) { result = 1;}
};
int main() {
int x = 0;
auto g = [](auto& result) { result = 1; };
std::thread(f<int>(), std::ref(x)); // COMPILES
std::thread(g, std::ref(x)); // FAILS TO COMPILE
}
त्रुटि संदेश:
मैं नीचे करने के लिए संकलन त्रुटि आसुत किया है
In file included from /usr/include/c++/4.9/thread:39:0,
from foo.cpp:1:
/usr/include/c++/4.9/functional: In instantiation of ‘struct std::_Bind_simple<main()::<lambda(auto:1&)>(std::reference_wrapper<int>)>’:
/usr/include/c++/4.9/thread:140:47: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = main()::<lambda(auto:1&)>&; _Args = {std::reference_wrapper<int>}]’
foo.cpp:13:31: required from here
/usr/include/c++/4.9/functional:1665:61: error: no type named ‘type’ in ‘class std::result_of<main()::<lambda(auto:1&)>(std::reference_wrapper<int>)>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
/usr/include/c++/4.9/functional:1695:9: error: no type named ‘type’ in ‘class std::result_of<main()::<lambda(auto:1&)>(std::reference_wrapper<int>)>’
_M_invoke(_Index_tuple<_Indices...>)
^
मेरे संकलक संस्करण
$ g++ --version
g++ (Ubuntu 4.9.1-16ubuntu6) 4.9.1
संकलन लैम्ब्डा के लिए क्यों असफल है लेकिन मज़ेदार नहीं?
संपादित करें: मैं जेनेरिक लैम्ब्डा के साथ क्या कर रहा हूं (रेफरी को असाइन करना) कैसे प्राप्त कर सकता हूं?
'एफ' जेनेरिक लैम्ब्डा के बराबर नहीं है। टेम्पलेट 'ऑपरेटर()() 'होने से अधिक सटीक होगा (और यह वही त्रुटि उत्पन्न करेगा)। – 0x499602D2
'std :: bind' को लक्ष्य फ़ंक्शन पर पास करने से पहले' context_wrapper' तर्कों को अनचाहे करने के लिए निर्दिष्ट किया गया है, इसलिए ['std :: thread (std :: bind (g, std :: ref (x)))' के रूप में कार्य करता है आप इरादा रखते हैं] (http://coliru.stacked-crooked.com/a/3f908c16c78c4027)। – Casey