tr1 https://www.e-learn.cn/tag/tr1 zh-hans Link TR1 on Ubuntu? https://www.e-learn.cn/topic/3101747 <span>Link TR1 on Ubuntu?</span> <span><span lang="" about="/user/53" typeof="schema:Person" property="schema:name" datatype="">依然范特西╮</span></span> <span>2020-01-05 00:41:43</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I have written a class using std::tr1::regex, and I don't know how to link it. I get (sorry for the large dump...) : </p> <pre><code>$ g++ DictReader.cpp -std=c++0x /usr/include/c++/4.4/tr1_impl/regex:2255: warning: inline function ‘bool std::tr1::regex_search(_Bi_iter, _Bi_iter, std::tr1::match_results&lt;_Bi_iter, _Allocator&gt;&amp;, const std::tr1::basic_regex&lt;_Ch_type, _Rx_traits&gt;&amp;, std::tr1::regex_constants::match_flag_type) [with _Bi_iter = __gnu_cxx::__normal_iterator&lt;const char*, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, _Allocator = std::allocator&lt;std::tr1::sub_match&lt;__gnu_cxx::__normal_iterator&lt;const char*, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt; &gt;, _Ch_type = char, _Rx_traits = std::tr1::regex_traits&lt;char&gt;]’ used but never defined /usr/lib/gcc/x86_64-linux-gnu/4.4.1/../../../../lib/crt1.o: In function `_start': /build/buildd/eglibc-2.10.1/csu/../sysdeps/x86_64/elf/start.S:109: undefined reference to `main' /tmp/ccgBkWlK.o: In function `DictReader::operator++(int)': DictReader.cpp:(.text+0xb2a): undefined reference to `bool std::tr1::regex_search&lt;__gnu_cxx::__normal_iterator&lt;char const*, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::allocator&lt;std::tr1::sub_match&lt;__gnu_cxx::__normal_iterator&lt;char const*, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt; &gt;, char, std::tr1::regex_traits&lt;char&gt; &gt;(__gnu_cxx::__normal_iterator&lt;char const*, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, __gnu_cxx::__normal_iterator&lt;char const*, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::tr1::match_results&lt;__gnu_cxx::__normal_iterator&lt;char const*, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::allocator&lt;std::tr1::sub_match&lt;__gnu_cxx::__normal_iterator&lt;char const*, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt; &gt; &gt;&amp;, std::tr1::basic_regex&lt;char, std::tr1::regex_traits&lt;char&gt; &gt; const&amp;, std::bitset&lt;11ul&gt;)' /tmp/ccgBkWlK.o: In function `std::tr1::basic_regex&lt;char, std::tr1::regex_traits&lt;char&gt; &gt;::basic_regex(char const*, unsigned int)': DictReader.cpp:(.text._ZNSt3tr111basic_regexIcNS_12regex_traitsIcEEEC1EPKcj[std::tr1::basic_regex&lt;char, std::tr1::regex_traits&lt;char&gt; &gt;::basic_regex(char const*, unsigned int)]+0x75): undefined reference to `std::tr1::basic_regex&lt;char, std::tr1::regex_traits&lt;char&gt; &gt;::_M_compile()' /tmp/ccgBkWlK.o: In function `std::tr1::basic_regex&lt;char, std::tr1::regex_traits&lt;char&gt; &gt;::basic_regex(std::tr1::basic_regex&lt;char, std::tr1::regex_traits&lt;char&gt; &gt; const&amp;)': DictReader.cpp:(.text._ZNSt3tr111basic_regexIcNS_12regex_traitsIcEEEC1ERKS3_[std::tr1::basic_regex&lt;char, std::tr1::regex_traits&lt;char&gt; &gt;::basic_regex(std::tr1::basic_regex&lt;char, std::tr1::regex_traits&lt;char&gt; &gt; const&amp;)]+0x60): undefined reference to `std::tr1::basic_regex&lt;char, std::tr1::regex_traits&lt;char&gt; &gt;::_M_compile()' collect2: ld returned 1 exit status. </code></pre> <p>What should I link against? </p> <br /><h3>回答1:</h3><br /><p>This is a linker error, it does not found the function you are using. TR1 is still new so it may not be implemented everywhere. I recommend you to use the boost regex library instead. </p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/2398994/link-tr1-on-ubuntu</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c-0" hreflang="zh-hans">c++</a></div> <div class="field--item"><a href="/tag/tr1" hreflang="zh-hans">tr1</a></div> </div> </div> Sat, 04 Jan 2020 16:41:43 +0000 依然范特西╮ 3101747 at https://www.e-learn.cn Link TR1 on Ubuntu? https://www.e-learn.cn/topic/3101737 <span>Link TR1 on Ubuntu?</span> <span><span lang="" about="/user/150" typeof="schema:Person" property="schema:name" datatype="">只谈情不闲聊</span></span> <span>2020-01-05 00:40:08</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I have written a class using std::tr1::regex, and I don't know how to link it. I get (sorry for the large dump...) : </p> <pre><code>$ g++ DictReader.cpp -std=c++0x /usr/include/c++/4.4/tr1_impl/regex:2255: warning: inline function ‘bool std::tr1::regex_search(_Bi_iter, _Bi_iter, std::tr1::match_results&lt;_Bi_iter, _Allocator&gt;&amp;, const std::tr1::basic_regex&lt;_Ch_type, _Rx_traits&gt;&amp;, std::tr1::regex_constants::match_flag_type) [with _Bi_iter = __gnu_cxx::__normal_iterator&lt;const char*, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, _Allocator = std::allocator&lt;std::tr1::sub_match&lt;__gnu_cxx::__normal_iterator&lt;const char*, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt; &gt;, _Ch_type = char, _Rx_traits = std::tr1::regex_traits&lt;char&gt;]’ used but never defined /usr/lib/gcc/x86_64-linux-gnu/4.4.1/../../../../lib/crt1.o: In function `_start': /build/buildd/eglibc-2.10.1/csu/../sysdeps/x86_64/elf/start.S:109: undefined reference to `main' /tmp/ccgBkWlK.o: In function `DictReader::operator++(int)': DictReader.cpp:(.text+0xb2a): undefined reference to `bool std::tr1::regex_search&lt;__gnu_cxx::__normal_iterator&lt;char const*, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::allocator&lt;std::tr1::sub_match&lt;__gnu_cxx::__normal_iterator&lt;char const*, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt; &gt;, char, std::tr1::regex_traits&lt;char&gt; &gt;(__gnu_cxx::__normal_iterator&lt;char const*, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, __gnu_cxx::__normal_iterator&lt;char const*, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::tr1::match_results&lt;__gnu_cxx::__normal_iterator&lt;char const*, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::allocator&lt;std::tr1::sub_match&lt;__gnu_cxx::__normal_iterator&lt;char const*, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt; &gt; &gt;&amp;, std::tr1::basic_regex&lt;char, std::tr1::regex_traits&lt;char&gt; &gt; const&amp;, std::bitset&lt;11ul&gt;)' /tmp/ccgBkWlK.o: In function `std::tr1::basic_regex&lt;char, std::tr1::regex_traits&lt;char&gt; &gt;::basic_regex(char const*, unsigned int)': DictReader.cpp:(.text._ZNSt3tr111basic_regexIcNS_12regex_traitsIcEEEC1EPKcj[std::tr1::basic_regex&lt;char, std::tr1::regex_traits&lt;char&gt; &gt;::basic_regex(char const*, unsigned int)]+0x75): undefined reference to `std::tr1::basic_regex&lt;char, std::tr1::regex_traits&lt;char&gt; &gt;::_M_compile()' /tmp/ccgBkWlK.o: In function `std::tr1::basic_regex&lt;char, std::tr1::regex_traits&lt;char&gt; &gt;::basic_regex(std::tr1::basic_regex&lt;char, std::tr1::regex_traits&lt;char&gt; &gt; const&amp;)': DictReader.cpp:(.text._ZNSt3tr111basic_regexIcNS_12regex_traitsIcEEEC1ERKS3_[std::tr1::basic_regex&lt;char, std::tr1::regex_traits&lt;char&gt; &gt;::basic_regex(std::tr1::basic_regex&lt;char, std::tr1::regex_traits&lt;char&gt; &gt; const&amp;)]+0x60): undefined reference to `std::tr1::basic_regex&lt;char, std::tr1::regex_traits&lt;char&gt; &gt;::_M_compile()' collect2: ld returned 1 exit status. </code></pre> <p>What should I link against? </p> <br /><h3>回答1:</h3><br /><p>This is a linker error, it does not found the function you are using. TR1 is still new so it may not be implemented everywhere. I recommend you to use the boost regex library instead. </p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/2398994/link-tr1-on-ubuntu</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c-0" hreflang="zh-hans">c++</a></div> <div class="field--item"><a href="/tag/tr1" hreflang="zh-hans">tr1</a></div> </div> </div> Sat, 04 Jan 2020 16:40:08 +0000 只谈情不闲聊 3101737 at https://www.e-learn.cn How does one include TR1? https://www.e-learn.cn/topic/3026481 <span>How does one include TR1?</span> <span><span lang="" about="/user/70" typeof="schema:Person" property="schema:name" datatype="">蹲街弑〆低调</span></span> <span>2019-12-31 11:27:52</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>Different compilers seem to have different ideas about TR1. G++ only seems to accept includes of the type:</p> <pre><code>#include &lt;tr1/unordered_map&gt; #include &lt;tr1/memory&gt; ... </code></pre> <p>While Microsofts compiler only accept:</p> <pre><code>#include &lt;unordered_map&gt; #include &lt;memory&gt; ... </code></pre> <p>As for as I understand TR1, the Microsoft way is the correct one.</p> <p>Is there a way to get G++ to accept the second version? How does one in general handle TR1 in a portable way?</p> <br /><h3>回答1:</h3><br /><p>Install boost on your machine.<br /> Add the following directory to your search path.</p> <p>&lt;Boost Install Directory&gt;/boost/tr1/tr1</p> <p>see here boost tr1 for details</p> <p>Now when you include &lt;memory&gt; you get the tr1 version of memory that has std::tr1::shared_ptr and then it includes the platform specific version of &lt;memory&gt; to get all the normal goodies. </p> <br /><br /><br /><h3>回答2:</h3><br /><pre><code>#ifdef _WIN32 #include &lt;unordered_map&gt; #include &lt;memory&gt; #else #include &lt;tr1/unordered_map&gt; #include &lt;trl/memory&gt; #endif </code></pre> <br /><br /><br /><h3>回答3:</h3><br /><p>Perhaps the best way would be to simply use boost libraries for now, as in many cases they have alternatives with a similar interface to TR1 features, and are just in a different (but consistent) header path and namespace. This has the advantage of working on compilers that haven't even <em>begun</em> implementing C++0x. And there are plenty of useful boost libraries that aren't in TR1 at all :)</p> <p>Alternately, on G++, you could try passing --std=gnu++0x on the command line. This works for &lt;unordered_set&gt; and &lt;unordered_map&gt;, at least. Then to make it available in std::tr1:</p> <pre><code>namespace std { namespace tr1 { using namespace std; } } </code></pre> <p>This is evil, naturally. I highly recommend the boost approach instead :)</p> <br /><br /><br /><h3>回答4:</h3><br /><p>A tad hacky perhaps, but you could simply add the compiler tr1 directory to your include path.</p> <br /><br /><br /><h3>回答5:</h3><br /><p>If under Windows, add the 'tr1' directory to the system path. Then <code>#include &lt;memory&gt;</code> should work.</p> <br /><br /><br /><h3>回答6:</h3><br /><p>I asked myself the same question. Unfortunately, the technical report doesn't say how the headers should be included. It only defines that the extensions should be in the ::std::tr1 namespace.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/1228402/how-does-one-include-tr1</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c-0" hreflang="zh-hans">c++</a></div> <div class="field--item"><a href="/tag/include" hreflang="zh-hans">include</a></div> <div class="field--item"><a href="/tag/c11" hreflang="zh-hans">c++11</a></div> <div class="field--item"><a href="/tag/portability" hreflang="zh-hans">portability</a></div> <div class="field--item"><a href="/tag/tr1" hreflang="zh-hans">tr1</a></div> </div> </div> Tue, 31 Dec 2019 03:27:52 +0000 蹲街弑〆低调 3026481 at https://www.e-learn.cn Difference in performance between map and unordered_map in c++ https://www.e-learn.cn/topic/2995258 <span>Difference in performance between map and unordered_map in c++</span> <span><span lang="" about="/user/137" typeof="schema:Person" property="schema:name" datatype="">独自空忆成欢</span></span> <span>2019-12-30 01:46:27</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I have a simple requirement, i need a map of type . however i need fastest theoretically possible retrieval time.</p> <p>i used both map and the new proposed unordered_map from tr1 i found that at least while parsing a file and creating the map, by inserting an element at at time. </p> <p>map took only 2 minutes while unordered_map took 5 mins.</p> <p>As i it is going to be part of a code to be executed on Hadoop cluster and will contain ~100 million entries, i need smallest possible retrieval time.</p> <p>Also another helpful information: currently the data (keys) which is being inserted is range of integers from 1,2,... to ~10 million.</p> <p>I can also impose user to specify max value and to use order as above, will that significantly effect my implementation? (i heard map is based on rb trees and inserting in increasing order leads to better performance (or worst?) )</p> <p>here is the code </p> <pre><code>map&lt;int,int&gt; Label // this is being changed to unordered_map fstream LabelFile("Labels.txt"); // Creating the map from the Label.txt if (LabelFile.is_open()) { while (! LabelFile.eof() ) { getline (LabelFile,inputLine); try { curnode=inputLine.substr(0,inputLine.find_first_of("\t")); nodelabel=inputLine.substr(inputLine.find_first_of("\t")+1,inputLine.size()-1); Label[atoi(curnode.c_str())]=atoi(nodelabel.c_str()); } catch(char* strerr) { failed=true; break; } } LabelFile.close(); } </code></pre> <p>Tentative Solution: After review of comments and answers, i believe a Dynamic C++ array would be the best option, since the implementation will use dense keys. Thanks</p> <br /><h3>回答1:</h3><br /><p>Insertion for unordered_map should be <strong><em>O(1)</em></strong> and retrieval should be roughly <strong><em>O(1)</em></strong>, (its essentially a hash-table). </p> <p>Your timings as a result are way <strong>OFF</strong>, or there is something <strong>WRONG</strong> with your implementation or usage of unordered_map. </p> <p>You need to provide some more information, and possibly how you are using the container.</p> <p>As per section 6.3 of n1836 the complexities for insertion/retreival are given:</p> <ul><li>http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf</li> </ul><p>One issue you should consider is that your implementation may need to continually be <strong><em>rehashing</em></strong> the structure, as you say you have <em>100mil+ items</em>. In that case when instantiating the container, if you have a rough idea about how many <em>"unique"</em> elements will be inserted into the container, you can pass that in as a parameter to the constructor and the container will be instantiated accordingly with a bucket-table of appropriate size.</p> <br /><br /><br /><h3>回答2:</h3><br /><p>The extra time loading the unordered_map is due to dynamic array resizing. The resizing schedule is to double the number of cells each when the table exceeds it's load factor. So from an empty table, expect O(lg n) copies of the entire data table. You can eliminate these extra copies by sizing the hash table upfront. Specifically</p> <pre><code>Label.reserve(expected_number_of_entries / Label.max_load_factor()); </code></pre> <p>Dividing by the max_load_factor is to account for the empty cells that are necessary for the hash table to operate.</p> <br /><br /><br /><h3>回答3:</h3><br /><p>unordered_map (at least in most implementations) gives fast retrieval, but relatively poor insertion speed compared to map. A tree is generally at its best when the data is randomly ordered, and at its worst when the data is ordered (you constantly insert at one end of the tree, increasing the frequency of re-balancing).</p> <p>Given that it's ~10 million total entries, you could just allocate a large enough array, and get really fast lookups -- assuming enough physical memory that it didn't cause thrashing, but that's not a huge amount of memory by modern standards.</p> <p>Edit: yes, a vector is basically a dynamic array.</p> <p>Edit2: The code you've added some some problems. Your <code>while (! LabelFile.eof() )</code> is broken. You normally want to do something like <code>while (LabelFile &gt;&gt; inputdata)</code> instead. You're also reading the data somewhat inefficiently -- what you apparently expecting is two numbers separated by a tab. That being the case, I'd write the loop something like:</p> <pre><code>while (LabelFile &gt;&gt; node &gt;&gt; label) Label[node] = label; </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/2350248/difference-in-performance-between-map-and-unordered-map-in-c</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c-0" hreflang="zh-hans">c++</a></div> <div class="field--item"><a href="/tag/data-structures" hreflang="zh-hans">data-structures</a></div> <div class="field--item"><a href="/tag/stl" hreflang="zh-hans">stl</a></div> <div class="field--item"><a href="/tag/tr1" hreflang="zh-hans">tr1</a></div> </div> </div> Sun, 29 Dec 2019 17:46:27 +0000 独自空忆成欢 2995258 at https://www.e-learn.cn How does weak_ptr work? https://www.e-learn.cn/topic/2968414 <span>How does weak_ptr work?</span> <span><span lang="" about="/user/151" typeof="schema:Person" property="schema:name" datatype="">时间秒杀一切</span></span> <span>2019-12-28 02:26:26</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I understand how to use <code>weak_ptr</code> and <code>shared_ptr</code>. I understand how <code>shared_ptr</code> works, by counting the number of references in its object. How does <code>weak_ptr</code> work? I tried reading through the boost source code, and I'm not familiar enough with boost to understand all the things it uses.</p> <p>Thanks.</p> <br /><h3>回答1:</h3><br /><p><code>shared_ptr</code> uses an extra "counter" object (aka. "shared count" or "control block") to store the reference count. (BTW: that "counter" object also stores the deleter.)</p> <p>Every <code>shared_ptr</code> and <code>weak_ptr</code> contains a pointer to the actual pointee, and a second pointer to the "counter" object.</p> <p>To implement <code>weak_ptr</code>, the "counter" object stores two different counters:</p> <ul><li>The "use count" is the number of <code>shared_ptr</code> instances pointing to the object.</li> <li>The "weak count" is the number of <code>weak_ptr</code> instances pointing to the object, plus one if the "use count" is still &gt; 0.</li> </ul><p>The pointee is deleted when the "use count" reaches zero.</p> <p>The "counter" helper object is deleted when the "weak count" reaches zero (which means the "use count" must also be zero, see above).</p> <p>When you try to obtain a <code>shared_ptr</code> from a <code>weak_ptr</code>, the library atomically checks the "use count", and if it's &gt; 0 increments it. If that succeeds you get your <code>shared_ptr</code>. If the "use count" was already zero you get an empty <code>shared_ptr</code> instance instead.</p> <hr /><p><strong>EDIT</strong>: Now, why do they add one to the weak count instead of just releasing the "counter" object when both counts drop to zero? Good question.</p> <p>The alternative would be to delete the "counter" object when both the "use count" and the "weak count" drop to zero. Here's the first reason: Checking two (pointer sized) counters atomically is not possible on every platform, and even where it is, it's more complicated than checking just one counter.</p> <p>Another reason is that the deleter must stay valid until it has finished executing. Since the deleter is stored in the "counter" object, that means the "counter" object must stay valid. Consider what could happen if there is one <code>shared_ptr</code> and one <code>weak_ptr</code> to some object, and they are reset at the same time in concurrent threads. Let's say the <code>shared_ptr</code> comes first. It decreases the "use count" to zero, and begins executing the deleter. Now the <code>weak_ptr</code> decreases the "weak count" to zero, and finds the "use count" is zero as well. So it deletes the "counter" object, and with it the deleter. While the deleter is still running.</p> <p>Of course there would be different ways to assure that the "counter" object stays alive, but I think increasing the "weak count" by one is a very elegant and intuitive solution. The "weak count" becomes the reference count for the "counter" object. And since <code>shared_ptr</code>s reference the counter object too, they too have to increment the "weak count".</p> <p>A probably even more intuitive solution would be to increment the "weak count" for every single <code>shared_ptr</code>, since every single <code>shared_ptr</code> hold's a reference to the "counter" object.</p> <p>Adding one for all <code>shared_ptr</code> instances is just an optimization (saves one atomic increment/decrement when copying/assigning <code>shared_ptr</code> instances).</p> <br /><br /><br /><h3>回答2:</h3><br /><p>Basically, a "weak_ptr" is an ordinary "T*" pointer that lets you RECOVER a strong reference, i.e. "shared_ptr", later in the code.</p> <p>Just like an ordinary T*, the weak_ptr doesn't do any reference-counting. Internally, to support reference-counting on an arbitrary type T, the STL (or any other library implementing this kind of logic) creates a wrapper object we'll call "Anchor". "Anchor" exists solely to implement the reference count and "when count is zero, call delete" behavior we need.</p> <p>In a strong reference, the shared_ptr implements its copy, operator=, constructor, destructor, and other pertinent APIs to update "Anchor"'s reference count. This is how a shared_ptr ensures that your "T" lives for exactly as long as somebody is using it. In a "weak_ptr", those same APIs just copy the actual Anchor ptr around. They do NOT update reference counts.</p> <p>This is why the most important APIs of "weak_ptr" are "expired" and the poorly-named "lock". "Expired" tells you if the underlying object is still around- i.e. "Has it already deleted itself because all strong references went out of scope?". "Lock" will (if it can) convert the weak_ptr to a strong reference shared_ptr, restoring reference-counting. </p> <p>BTW, "lock" is a terrible name for that API. You aren't (just) invoking a mutex, you're creating a strong reference from a weak one, with that "Anchor" acting. The biggest flaw in both templates is that they did not implement operator-&gt;, so to do anything with your object you have to recover the raw "T*". They did this mostly to support things like "shared_ptr", because primitive types don't support the "-&gt;" operator.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/5671241/how-does-weak-ptr-work</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c-0" hreflang="zh-hans">c++</a></div> <div class="field--item"><a href="/tag/boost" hreflang="zh-hans">boost</a></div> <div class="field--item"><a href="/tag/weak-references" hreflang="zh-hans">weak-references</a></div> <div class="field--item"><a href="/tag/tr1" hreflang="zh-hans">tr1</a></div> <div class="field--item"><a href="/tag/weak-ptr" hreflang="zh-hans">weak-ptr</a></div> </div> </div> Fri, 27 Dec 2019 18:26:26 +0000 时间秒杀一切 2968414 at https://www.e-learn.cn TR1 function multicast https://www.e-learn.cn/topic/2925530 <span>TR1 function multicast</span> <span><span lang="" about="/user/113" typeof="schema:Person" property="schema:name" datatype="">╄→尐↘猪︶ㄣ</span></span> <span>2019-12-25 05:19:11</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>How would you implement multicast for TR1 functors? I have my callback slots implemented like </p> <pre><code>void setCallback(std::tr1::function&lt;void (std::string)&gt; cb) { this-&gt;callback = cb; } </code></pre> <p>but need to pass more than one callback in one of them. I don't want to go into more complex solutions like observer, as this is the only case I need multicast so far. I also cannot use Boost.Signals (as suggested here), because I cannot use Boost. I don't need to explicitly handle disabling callback when subscriber no longer exist.</p> <br /><h3>回答1:</h3><br /><p>You most likely want:</p> <pre><code>void registerCallback(std::tr1::function&lt;void (std::string)&gt; cb) { this-&gt;callbacks.push_back(cb); } </code></pre> <p>with <code>callbacks</code> a container (whichever you like) of <code>std::tr1::function</code> objects instead of a single one. When dispatching, iterate over the callbacks.</p> <p>Also, if you want to be able to remove callbacks later, you can do something along these lines:</p> <pre><code>// I use list because I don't want the iterators to be invalid // after I add / remove elements std::list&lt;std::function&lt;void(std::string)&gt;&gt; callbacks; ... typedef std::list&lt;std::function&lt;void(std::string)&gt;&gt;::iterator callback_id; callback_id register_callback(std::function&lt;void(std::string)&gt; f) { return callbacks.insert(callbacks.end(), f); } void unregister_callback(callback_id id) { callbacks.erase(id); } </code></pre> <br /><br /><br /><h3>回答2:</h3><br /><p>Put them in a list / vector. If you have to remove them individually you have to wrap them some how (because they are not comparable).</p> <br /><br /><br /><h3>回答3:</h3><br /><p>Have a sequence of them rather than a single function (i.e. a <code>vector&lt;...&gt;</code>), and when calling back, iterate over the sequence and call.</p> <p>e.g</p> <pre><code>std::vector&lt;std::tr1::function&lt;void (std::string)&gt; &gt; callbacks; auto it = callbacks.begin(), end = callbacks.end(); for(; it != end; ++it) (*it)("somestring"); </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/7887582/tr1-function-multicast</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c-0" hreflang="zh-hans">c++</a></div> <div class="field--item"><a href="/tag/functor" hreflang="zh-hans">functor</a></div> <div class="field--item"><a href="/tag/tr1" hreflang="zh-hans">tr1</a></div> </div> </div> Tue, 24 Dec 2019 21:19:11 +0000 ╄→尐↘猪︶ㄣ 2925530 at https://www.e-learn.cn Workaround to allow tr1::function to swallow return values https://www.e-learn.cn/topic/2874611 <span>Workaround to allow tr1::function to swallow return values</span> <span><span lang="" about="/user/9" typeof="schema:Person" property="schema:name" datatype="">爷,独闯天下</span></span> <span>2019-12-24 10:55:41</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>As a follow-up to Can tr1::function swallow return values?, how can one work around the limitation that <code>tr1::function</code> cannot swallow return values?</p> <p>This should work in the specific case of swallowing the return value of a callable object taking no arguments:</p> <pre><code>template&lt;typename FuncT&gt; struct swallow_return_t { explicit swallow_return_t(FuncT i_Func):m_Func(i_Func){} void operator()(){ m_Func(); } FuncT m_Func; }; template&lt;typename FuncT&gt; swallow_return_t&lt;FuncT&gt; swallow_return(FuncT f) { return swallow_return_t&lt;FuncT&gt;(f); } </code></pre> <p>Then use like:</p> <pre><code>int Foo(); std::tr1::function&lt;void()&gt; Bar = swallow_return(Foo); </code></pre> <p>I assume variadic templates and perfect forwarding would permit generalization of this technique to arbitrary parameter lists. Is there a better way?</p> <br /><h3>回答1:</h3><br /><p>The following works for me in GCC 4.6.1:</p> <pre><code>#include &lt;functional&gt; int foo() { return 5; } int goo(double, char) { return 5; } int main() { std::function&lt;void()&gt; f = foo; std::function&lt;void(float, int)&gt; g = goo; (void)f(); (void)g(1.0f, 'a'); } </code></pre> <hr /><p>Here's a wrapper using lambdas, but it's not automagic yet</p> <pre><code>template &lt;typename T, typename ...Args&gt; struct strip_return { static inline std::function&lt;void(Args...)&gt; make_function(std::function&lt;T(Args...)&gt; f) { return [&amp;f](Args... args) -&gt; void { f(args...); }; } }; int main() { auto q = strip_return&lt;int&gt;::make_function(std::bind(foo)); q(); } </code></pre> <hr /><p>Forget the middle part. OK, since <code>std::function</code> is type-erasing, it's hard to get at the underlying types. However, if you go for the function reference directly, you can avoid these problems entirely:</p> <pre><code>template &lt;typename T, typename ...Args&gt; static inline std::function&lt;void(Args...)&gt; make_direct(T (&amp;f)(Args...)) { return [&amp;f](Args... args) -&gt; void { f(args...); }; } int main() { auto p = make_direct(foo); q(); } </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/6653531/workaround-to-allow-tr1function-to-swallow-return-values</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c-0" hreflang="zh-hans">c++</a></div> <div class="field--item"><a href="/tag/c11" hreflang="zh-hans">c++11</a></div> <div class="field--item"><a href="/tag/tr1" hreflang="zh-hans">tr1</a></div> </div> </div> Tue, 24 Dec 2019 02:55:41 +0000 爷,独闯天下 2874611 at https://www.e-learn.cn C++ how to handle tr1 and non-tr1 namespaces in portable code? https://www.e-learn.cn/topic/2854284 <span>C++ how to handle tr1 and non-tr1 namespaces in portable code?</span> <span><span lang="" about="/user/187" typeof="schema:Person" property="schema:name" datatype="">允我心安</span></span> <span>2019-12-24 05:04:05</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>Is there a canonical way to deal with the namespace issues that arise when trying to maintain portable code between a TR1 and non-TR1 toolchain?</p> <p>I have a VC++2010 project that <code>#include &lt;type_traits&gt;</code>. I also have an LLVM 3.0 compiler that can handle this fine. This allows me to use templates such as:</p> <pre><code>std::enable_if&lt;typename&gt; std::is_enum&lt;typename&gt; </code></pre> <p>However I also need to build and maintain this code on an Xcode 4.5 clang compiler:</p> <pre><code>$ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang --version Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn) Target: x86_64-apple-darwin11.4.2 Thread model: posix </code></pre> <p>This compiler doesn't seem to have a include file, instead it has a . However this is causing me problems because the namespace has changed from std:: to __gnu_cxx::, meaning I have to use:</p> <pre><code>__gnu_cxx::__enable_if&lt;typename&gt; </code></pre> <p>Somehow I was able to determine that the definition of the symbol <code>__GLIBCXX__</code> is sufficient to determine whether I should use one or the other (not even sure that's the right way to do it, but for now it works between the compilers I'm using).</p> <p>So I could resort to using preprocessor macros:</p> <pre><code>#ifdef __GLIBCXX__ # include &lt;tr1/type_traits&gt; # define ENABLE_IF __gnu_cxx::__enable_if #else # include &lt;type_traits&gt; # define ENABLE_IF std::enable_if #endif </code></pre> <p>But this seems like it might be more of a hack than a proper solution. (Actually I tried this and it doesn't work, because trying to use <code>__gnu_cxx::__enable_if</code> causes this error:</p> <pre><code>error: too few template arguments for class template '__enable_if' </code></pre> <ul><li>further digging suggests that this version of enable_if actually takes two template arguments. I'm now very lost...)</li> </ul><p>I thought about doing something like:</p> <pre><code>#ifdef __GLIBCXX__ # include &lt;tr1/type_traits&gt; namespace __gnu_cxx = foo; #else # include &lt;type_traits&gt; namespace std = foo; #endif ... foo::enable_if&lt; ... &gt; </code></pre> <p>However this doesn't work because the template is called <code>enable_if</code> in one namespace, but <code>__enable_if</code> in the other.</p> <p>I'm sure I'm not the first person to deal with this problem - can someone point me at the industry best practice for resolving this please? Or should I just use Boost instead?</p> <p>There is a similar question (I think) but only a partial answer here. Are there better options?</p> <p>EDIT: I tried this, with <code>&lt;boost/type_traits.hpp&gt;</code>:</p> <pre><code>#include &lt;boost/type_traits.hpp&gt; template &lt;typename ValueType&gt; class Extractor &lt;ValueType, typename boost::enable_if&lt;boost::is_enum&lt;ValueType&gt;::value&gt;::type&gt; { public: ValueType extract(double value) { return static_cast&lt;ValueType&gt;(static_cast&lt;int&gt;(value)); // cast to int first, then enum, to satisfy VC++2010 } }; enum MyEnum { Enum0, Enum1 }; Extractor&lt;MyEnum&gt; e; MyEnum ev = e.extract(1.0); </code></pre> <p>However this gives me the following compiler error in Xcode 4.5:</p> <pre><code>error: expected a qualified name after 'typename' class Extractor &lt;ValueType, typename boost::enable_if&lt;boost::is_enum&lt;ValueType&gt;::value&gt;::type&gt; { ^ error: unknown type name 'type' </code></pre> <p>So it doesn't seem that std::enable_if and boost::enable_if are drop-in compatible. </p> <br /><h3>回答1:</h3><br /><p>I'll answer my own question as I did get something working using <code>boost::enable_if_c</code> (note that the drop-in replacement for <code>std::enable_if</code> is <code>boost::enable_if_c</code>, not <code>boost::enable_if</code>).</p> <pre><code>#include &lt;boost/utility/enable_if.hpp&gt; #include &lt;boost/type_traits/is_enum.hpp&gt; // this would work except one of my environments doesn't contain &lt;complex&gt; so it's // too inclusive. Better (for me) to use the more specific includes above. // #include &lt;boost/type_traits.hpp&gt; template &lt;typename ValueType&gt; class Extractor &lt;ValueType, typename boost::enable_if_c&lt;boost::is_enum&lt;ValueType&gt;::value&gt;::type&gt; { public: ValueType extract(double value) { return static_cast&lt;ValueType&gt;(static_cast&lt;int&gt;(value)); // cast to int first, then enum, to satisfy VC++2010 } }; </code></pre> <p>However I'm still very curious to know whether there is a better way to deal with this than resorting to Boost.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/14823832/c-how-to-handle-tr1-and-non-tr1-namespaces-in-portable-code</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c-0" hreflang="zh-hans">c++</a></div> <div class="field--item"><a href="/tag/boost" hreflang="zh-hans">boost</a></div> <div class="field--item"><a href="/tag/namespaces" hreflang="zh-hans">namespaces</a></div> <div class="field--item"><a href="/tag/tr1" hreflang="zh-hans">tr1</a></div> <div class="field--item"><a href="/tag/typetraits" hreflang="zh-hans">typetraits</a></div> </div> </div> Mon, 23 Dec 2019 21:04:05 +0000 允我心安 2854284 at https://www.e-learn.cn Boost Serialization - Serialize std::tr1::shared_ptr? https://www.e-learn.cn/topic/2847590 <span>Boost Serialization - Serialize std::tr1::shared_ptr?</span> <span><span lang="" about="/user/144" typeof="schema:Person" property="schema:name" datatype="">核能气质少年</span></span> <span>2019-12-24 03:06:04</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>Boost::Serialization has builtin support for <code>boost::shared_ptr&lt;&gt;</code>.</p> <p>Is there a way to use this support for <code>std::tr1::shared_ptr&lt;&gt;</code> too?<br /> Is it possible to cast from <code>std::tr1::shared_ptr&lt;&gt;</code> to <code>boost::shared_ptr&lt;&gt;</code>?</p> <br /><h3>回答1:</h3><br /><p>A casting will not be possible as implementations differ. Also creating an instance of the one shared_ptr type with the value returned from get() on the other shared_ptr type will not work correctly as the reference countings will go to 0 at different points in your code which leads to deletion of the object before the last use of it. I am not an expert of boost::serialization but as the interfaces of std::tr1::shared_ptr&lt;&gt; and boost::shared_ptr&lt;&gt; are nearly identical it is very likely that you can just clone the serialization/deserialization code of boost::shared_ptr&lt;&gt; and only have to replace the namespace of the shared_ptr.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/4094604/boost-serialization-serialize-stdtr1shared-ptr</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c-0" hreflang="zh-hans">c++</a></div> <div class="field--item"><a href="/tag/boost" hreflang="zh-hans">boost</a></div> <div class="field--item"><a href="/tag/c11" hreflang="zh-hans">c++11</a></div> <div class="field--item"><a href="/tag/tr1" hreflang="zh-hans">tr1</a></div> <div class="field--item"><a href="/tag/boost-serialization" hreflang="zh-hans">boost-serialization</a></div> </div> </div> Mon, 23 Dec 2019 19:06:04 +0000 核能气质少年 2847590 at https://www.e-learn.cn out of range random number generation in C++ using tr1 https://www.e-learn.cn/topic/2845554 <span>out of range random number generation in C++ using tr1</span> <span><span lang="" about="/user/15" typeof="schema:Person" property="schema:name" datatype="">假装没事ソ</span></span> <span>2019-12-24 02:27:33</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I am trying to generate number from a uniform distribution of real number in the range of [0, 1). But compiler is generating numbers which are out of [0, 1) range. </p> <p>Here is the code:</p> <pre><code>int main(void) { // Solver solve; mt19937 mteng; mteng.seed(rdtsc()); uniform_real&lt;double&gt; uniRealD; double randomNum; for (int index = 0; index &lt; 10; index++){ randomNum = uniRealD(mteng); if(randomNum&lt;0.5) cout&lt;&lt;index&lt;&lt;" no. random number is: "&lt;&lt;randomNum&lt;&lt;endl; else cout&lt;&lt;"number generate is not in range"&lt;&lt;endl; } return 0; } </code></pre> <p>Any comments on what could be wrong with the code? I am using <code>rdtsc()</code> as seed.</p> <br /><h3>回答1:</h3><br /><p>I had to get rid of your seed function, <code>rdtsc()</code>, make some includes, introduce a <code>using namespace std</code>, and change a 0.5 to 1.0, and change <code>uniform_real</code> to <code>uniform_real_distribution</code>, but after that, using libc++, I get:</p> <pre><code>#include &lt;random&gt; #include &lt;iostream&gt; using namespace std; int main(void) { // Solver solve; mt19937 mteng; mteng.seed(0); uniform_real_distribution&lt;double&gt; uniRealD; double randomNum; for (int index = 0; index &lt; 10; index++){ randomNum = uniRealD(mteng); if(randomNum&lt;1.0) cout&lt;&lt;index&lt;&lt;" no. random number is: "&lt;&lt;randomNum&lt;&lt;endl; else cout&lt;&lt;"number generate is not in range"&lt;&lt;endl; } return 0; } 0 no. random number is: 0.592845 1 no. random number is: 0.844266 2 no. random number is: 0.857946 3 no. random number is: 0.847252 4 no. random number is: 0.623564 5 no. random number is: 0.384382 6 no. random number is: 0.297535 7 no. random number is: 0.056713 8 no. random number is: 0.272656 9 no. random number is: 0.477665 </code></pre> <br /><br /><br /><h3>回答2:</h3><br /><p>Your code shouldn't be doing that. Probably a bug in the implementation. What compiler and library versions? Try moving away from tr1 to C++11.</p> <br /><br /><br /><h3>回答3:</h3><br /><pre><code> if(randomNum&lt;0.5) cout&lt;&lt;index&lt;&lt;" no. random number is: "&lt;&lt;randomNum&lt;&lt;endl; else cout&lt;&lt;"number generate is not in range"&lt;&lt;endl; </code></pre> <p>Change the <code>if</code> statement to <code>if(randomNum &lt; 1.)</code></p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/10133832/out-of-range-random-number-generation-in-c-using-tr1</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c-0" hreflang="zh-hans">c++</a></div> <div class="field--item"><a href="/tag/random" hreflang="zh-hans">random</a></div> <div class="field--item"><a href="/tag/c11" hreflang="zh-hans">c++11</a></div> <div class="field--item"><a href="/tag/tr1" hreflang="zh-hans">tr1</a></div> <div class="field--item"><a href="/tag/uniform" hreflang="zh-hans">Uniform</a></div> </div> </div> Mon, 23 Dec 2019 18:27:33 +0000 假装没事ソ 2845554 at https://www.e-learn.cn