TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

C++ concepts support merged into GCC trunk

104 pointsby octoploidalmost 10 years ago

6 comments

cshimminalmost 10 years ago
For others like me who have been living under a rock:<p><a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Concepts_(C%2B%2B)" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Concepts_(C%2B%2B)</a>
评论 #10021769 未加载
评论 #10022581 未加载
qzncalmost 10 years ago
After watching this talk [0] by Andrei Alexandrescu, I became somewhat sceptical about concepts (and type classes and traits). Nevertheless, it&#x27;s probably an improvement for C++.<p>[0] <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=mCrVYYlFTrA" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=mCrVYYlFTrA</a>
评论 #10022192 未加载
kirabalmost 10 years ago
I&#x27;m really glad that concepts are finally making their way in after such a long time. Concepts make using and developing templates so much more straight-forward and easier to read!
评论 #10021755 未加载
jahnualmost 10 years ago
Is this just for experimentation or is it planned that this will be more or less the solution that makes it into the standard?
评论 #10021484 未加载
emcrazyonealmost 10 years ago
Like others I was struggling to find usefulness of this and what problems it solves. I admit, it seems like a stretch. There was a situation in which I needed a template class to be implemented such that I wanted only certain types to be accepted one way and others a different way.<p>I implemented a stream buffer that can accept different types into the stream. Based on the type, the buffer would serialize and expand to accept the data.<p>To put this more succintly, an insert of unit8_t type would put a single byte into the stream buffer and an insert of uint16_t would put two bytes into the stream buffer. And I needed a way to make sure the types were plain old data types (int, char, uint8_t, uint16_t, uint_32_t, etc...)<p>Here is sample code:<p>template&lt;typename T&gt; class TStream { private:<p><pre><code> typedef TBuffer&lt;T&gt; stream_buffer; stream_buffer mStream; </code></pre> public:<p><pre><code> TStream(); TStream(const TStream &amp;rhs); TStream::~TStream(); unsigned int Size() const; unsigned int ByteStreamLength() const; void Clear(); &#x2F;&#x2F; some methods omitted for brevity const T&amp;operator[](unsigned int idx); const T*operator[](unsigned int idx) const; TStream&lt;T&gt; &amp;operator=(const TStream &amp;rhs); TStream&lt;T&gt; &amp;operator=(T const); TStream&lt;T&gt; &amp;operator=(unsigned char); TStream&lt;T&gt; &amp;operator=(bool); TStream&lt;T&gt; &amp;operator=(double); &#x2F;&#x2F; rest omitted for brevity</code></pre> };<p>Doing this<p>TStream&lt;unsigned char&gt; ByteStream<p>causes ambiguity with<p>operator=(unsigned char).<p>I basically want operator=(unsigned char) to be omitted if T = unsigned char.<p>The way I solved this, without Concepts of course, was to use CRTP like so:<p>template&lt;typename D, typename T, typename U&gt;<p>struct implement_operator_plus_equals { implement_operator_plus_equals() { static_assert(std::is_base_of&lt;implement_operator_plus_equals, D&gt;::value, &quot;CRTP failure&quot; ); } D* self() { return static_cast&lt;D<i>&gt;(this); } D const</i> self() const { return static_cast&lt;D const<i>&gt;(this); }<p><pre><code> typedef D Self; Self&amp; operator+=( U u ) { static_assert( (sizeof(U)%sizeof(T)) == 0, &quot;Non integer number of Ts in a U&quot; ); T* b = reinterpret_cast&lt;T*&gt;(&amp;u); T* e = b + sizeof(U)&#x2F;sizeof(T); for (T* it = b; it != e; ++it) { self()-&gt;consume(*it); return *self(); }</code></pre> }; template&lt;typename D, typename T&gt; struct implement_operator_plus_equals&lt;D,T,T&gt; { implement_operator_plus_equals() { static_assert( std::is_base_of&lt;implement_operator_plus_equals, D&gt;::value, &quot;CRTP failure&quot; ); &#x2F;&#x2F; Have an operator+= that cannot be called, so using ...::operator+= is legal, &#x2F;&#x2F; but mostly harmless: class block_call {}; void operator+=( block_call ) {} } };<p>I believe I could have solved this without CRTP using Concepts. Although my understanding is that Concepts follows a methodology of deny all, allow what is defined and there appears to be no way to allow all, deny what is specified as a Concept. In other words, Concepts only tell the compiler what is allowed and there appears to be no way to specify what is NOT allowed without some further &quot;trickery.&quot;
smegelalmost 10 years ago
So type checking for thing inside the &lt;&gt; as well as the ()?
评论 #10021552 未加载
评论 #10021384 未加载
评论 #10023318 未加载