<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5683827728680597230</id><updated>2011-07-30T13:47:08.870-05:00</updated><category term='c++'/><title type='text'>A Still More Perfect State</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://stillmoreperfect.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5683827728680597230/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://stillmoreperfect.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>dmitchell</name><uri>http://www.blogger.com/profile/04848881387975069067</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>3</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5683827728680597230.post-7681710231566061475</id><published>2010-07-19T19:18:00.014-05:00</published><updated>2010-08-03T18:03:52.791-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><title type='text'>Unpacking tuples for function arguments</title><content type='html'>&lt;p&gt;Sometimes you have a tuple and want to pass its contents to a function as arguments. One way is to overload the function:&lt;/p&gt;
&lt;pre class="brush: cpp"&gt;int average(int a, int b, int c)
{
  return (a + b + c) / 3;
}

int average(std::tuple&amp;lt;int,int,int&amp;gt; x)
{
  return average(std::get&amp;lt;0&amp;gt;(x), std::get&amp;lt;1&amp;gt;(x), std::get&amp;lt;2&amp;gt;(x));
}&lt;/pre&gt;
&lt;p&gt;This works for an individual function or two, but it isn't a general solution and it doesn't scale. So let's automate it.&lt;/p&gt;
&lt;pre class="brush: cpp"&gt;#include &amp;lt;functional&amp;gt; 
#include &amp;lt;tuple&amp;gt; 

namespace detail { 

template&amp;lt;std::size_t... Indices&amp;gt;
struct indices { typedef indices&amp;lt;Indices...,sizeof...(Indices)&amp;gt; next; }; 

// Generates the type indices&lt;0,1,...,N-1&gt; which is used to unpack an N-tuple.
template&amp;lt;std::size_t N&amp;gt;
struct make_indices { typedef typename make_indices&amp;lt;N-1&amp;gt;::type::next type; };

template&amp;lt;&amp;gt;
struct make_indices&amp;lt;0&amp;gt; { typedef indices&amp;lt;&amp;gt; type; };

// We use the indices to unpack the tuple.
template&amp;lt;class Function, class... T, std::size_t... Indices&amp;gt; 
inline typename std::result_of&amp;lt;Function(T...)&amp;gt;::type 
apply(Function f, std::tuple&amp;lt;T...&amp;gt;&amp;&amp; x, indices&amp;lt;Indices...&amp;gt;) 
{ 
  return f(std::forward&amp;lt;T&amp;gt;(std::get&amp;lt;Indices&amp;gt;(x))...); 
} 

} // namespace detail 

// Apply function f to the contents of tuple x.
template&amp;lt;class Function, class... T&amp;gt; 
inline typename std::result_of&amp;lt;Function(T...)&amp;gt;::type 
apply(Function f, std::tuple&amp;lt;T...&amp;gt;&amp;&amp; x) 
{ 
  typedef typename detail::make_indices&amp;lt;sizeof...(T)&amp;gt;::type Indices; 
  return detail::apply(f, std::forward&amp;lt;std::tuple&amp;lt;T...&amp;gt;&amp;gt;(x), Indices()); 
}&lt;/pre&gt;
&lt;p&gt;Now we can call our functions this way:&lt;/p&gt;
&lt;pre class="brush: cpp"&gt;int average(int a, int b, int c) 
{ 
  return (a + b + c) / 3; 
} 

int zero() 
{ 
  return 0; 
} 

int main() 
{ 
  std::cout &amp;lt;&amp;lt; apply(average, std::make_tuple(1, 2, 3)) &amp;lt;&amp;lt; '\n'; 

  std::cout &amp;lt;&amp;lt; apply(zero, std::make_tuple()) &amp;lt;&amp;lt; '\n'; 

  std::cout &amp;lt;&amp;lt; apply([](int x, int y){ return x + y; }, std::make_tuple(2, 3)) &amp;lt;&amp;lt; '\n'; 
}&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5683827728680597230-7681710231566061475?l=stillmoreperfect.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://stillmoreperfect.blogspot.com/feeds/7681710231566061475/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://stillmoreperfect.blogspot.com/2010/07/unpacking-tuples-for-function-arguments.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5683827728680597230/posts/default/7681710231566061475'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5683827728680597230/posts/default/7681710231566061475'/><link rel='alternate' type='text/html' href='http://stillmoreperfect.blogspot.com/2010/07/unpacking-tuples-for-function-arguments.html' title='Unpacking tuples for function arguments'/><author><name>dmitchell</name><uri>http://www.blogger.com/profile/04848881387975069067</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5683827728680597230.post-8913538494533088209</id><published>2010-03-11T17:20:00.008-06:00</published><updated>2010-08-03T18:03:41.406-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><title type='text'>Curious</title><content type='html'>&lt;p&gt;In designs that use the curiously recurring template pattern, the base class usually needs access to the derived class. Typically this is done by casting the base class's &lt;code&gt;this&lt;/code&gt; pointer. This technique is encapsulated in the &lt;code&gt;curious&lt;/code&gt; class below. Simply derive the base class from &lt;code&gt;curious&lt;/code&gt; to inherit a &lt;code&gt;derived()&lt;/code&gt; member function that returns a pointer to the derived class.&lt;/p&gt;

&lt;pre class="brush: cpp"&gt;template&amp;lt;class Derived&amp;gt;
class curious {
protected:

  typedef Derived derived_type;

  derived_type* derived()
  {
    return static_cast&amp;lt;derived_type*&amp;gt;(this);
  }

  derived_type const* derived() const
  {
    return static_cast&amp;lt;derived_type const*&amp;gt;(this);
  }
};&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5683827728680597230-8913538494533088209?l=stillmoreperfect.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://stillmoreperfect.blogspot.com/feeds/8913538494533088209/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://stillmoreperfect.blogspot.com/2010/03/curious.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5683827728680597230/posts/default/8913538494533088209'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5683827728680597230/posts/default/8913538494533088209'/><link rel='alternate' type='text/html' href='http://stillmoreperfect.blogspot.com/2010/03/curious.html' title='Curious'/><author><name>dmitchell</name><uri>http://www.blogger.com/profile/04848881387975069067</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5683827728680597230.post-3268363963469139284</id><published>2010-03-09T13:31:00.032-06:00</published><updated>2010-08-03T18:03:10.631-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><title type='text'>Compile-time logic</title><content type='html'>&lt;p&gt;Sometimes it is helpful to compute the result of logical operations like &lt;code&gt;and&lt;/code&gt; and &lt;code&gt;or&lt;/code&gt; at compile-time. These operations return a boolean result, so the first thing a compile-time logic library needs is classes to represent true and false results. Here is one way to do it.&lt;/p&gt;
&lt;pre class="brush: cpp"&gt;template&amp;lt;bool c&amp;gt;
struct bool_ {

  typedef bool_ type;

  typedef bool value_type;

  static bool const value = c;

  operator bool() const { return c; }
};

typedef bool_&amp;lt;true&amp;gt; true_;

typedef bool_&amp;lt;false&amp;gt; false_;&lt;/pre&gt;
&lt;p&gt;It would also have been possible to simply write&lt;/p&gt;
&lt;pre class="brush: cpp"&gt;struct true_ { };

struct false_ { };&lt;/pre&gt;
&lt;p&gt;but the nested &lt;code&gt;typedef&lt;/code&gt;s and &lt;code&gt;bool&lt;/code&gt; will be helpful later on. The implicit conversion to &lt;code&gt;bool&lt;/code&gt; is occasionally helpful but code that uses it can always be rewritten to make it unnecessary, for instance by using tag dispatching.&lt;/p&gt;
&lt;p&gt;Before we continue with the presentation of the code, let's describe the algorithms we are writing. In the case of the short-circuit &lt;code&gt;and&lt;/code&gt;, it is this: if the first argument is false, then the result is false; otherwise it is the result of applying short-circuit &lt;code&gt;and&lt;/code&gt; to the remaining arguments. If there is only one argument, then the result is the same as the argument. The short-circuit &lt;code&gt;or&lt;/code&gt; is similar: if the first argument is true, then the result is true; otherwise it is the result of applying short-circuit &lt;code&gt;or&lt;/code&gt; to the remaining arguments. If there is only one argument, then the result is the same as the argument. Note that as stated these algorithms apply even to the logical &lt;code&gt;and&lt;/code&gt; or &lt;code&gt;or&lt;/code&gt; of a single argument, but for consistency with the built-in &lt;code&gt;&amp;&amp;&lt;/code&gt; and &lt;code&gt;||&lt;/code&gt; operators we will require at least two arguments.&lt;/p&gt;
&lt;p&gt;It should be clear from the statement of the algorithms that we need some means of branching, that is to say some means of representing an &lt;code&gt;if-then-else&lt;/code&gt; construct. This is accomplished by the &lt;code&gt;if_&lt;/code&gt; class. &lt;code&gt;if_&lt;/code&gt; has three template parameters: a boolean condition and two types. If the condition is true, then &lt;code&gt;if_&lt;/code&gt; selects the first type; otherwise it selects the second type.&lt;/p&gt;
&lt;pre class="brush: cpp"&gt;template&amp;lt;bool c, class T1, class T2&amp;gt;
struct if_c { typedef T1 type; };

template&amp;lt;class T1, class T2&amp;gt;
struct if_c&amp;lt;false,T1,T2&amp;gt; { typedef T2 type; };

template&amp;lt;class C, class T1, class T2&amp;gt;
struct if_ : if_c&amp;lt;C::value,T1,T2&amp;gt; { };&lt;/pre&gt;
&lt;p&gt;There are two versions of &lt;code&gt;if_&lt;/code&gt;. The first is called &lt;code&gt;if_c&lt;/code&gt; and uses a &lt;code&gt;bool&lt;/code&gt; template parameter for its condition. The second is called &lt;code&gt;if_&lt;/code&gt; and uses a class with a nested &lt;code&gt;value&lt;/code&gt; for its condition; because &lt;code&gt;value&lt;/code&gt; is passed to &lt;code&gt;if_c&lt;/code&gt;, it must be a type convertible to &lt;code&gt;bool&lt;/code&gt; at compile-time.&lt;/p&gt;
&lt;p&gt;Now we understand the purpose of the &lt;code&gt;value&lt;/code&gt; nested in &lt;code&gt;bool_&lt;/code&gt;. It allows that class to be used with &lt;code&gt;if_&lt;/code&gt; as a condition.&lt;/p&gt;
&lt;p&gt;We are nearing the finish line now, but before we continue let's step back from the details of the code and take a high-level look at what we intend to achieve. We are going to write a class named &lt;code&gt;and_&lt;/code&gt; that takes two or more template arguments, computes the short-circuit logical &lt;code&gt;and&lt;/code&gt; of the arguments, and exposes the result as a nested type named &lt;code&gt;type&lt;/code&gt; that will be a &lt;code&gt;typedef&lt;/code&gt; for either &lt;code&gt;true_&lt;/code&gt; or &lt;code&gt;false_&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;We already described the algorithm that computes the result; now we describe its implementation: if the first argument to &lt;code&gt;and_&lt;/code&gt; is false, then &lt;code&gt;and_&lt;/code&gt; inherits &lt;code&gt;false_&lt;/code&gt;; otherwise it inherits the result of applying &lt;code&gt;and_&lt;/code&gt; to the remainder of its arguments. Now we understand the purpose of the type &lt;code&gt;type&lt;/code&gt; nested in &lt;code&gt;bool_&lt;/code&gt;: when &lt;code&gt;and_&lt;/code&gt; inherits &lt;code&gt;false_&lt;/code&gt; (or &lt;code&gt;true_&lt;/code&gt;, as the case may be), it also inherits the appropriate nested type.&lt;/p&gt;
&lt;p&gt;We intend to make &lt;code&gt;and_&lt;/code&gt; inherit from one class or another based on a condition, but this is useful behavior in its own right so let's encapsulate it in a class named &lt;code&gt;eval_if&lt;/code&gt;. Like &lt;code&gt;if_&lt;/code&gt;, &lt;code&gt;eval_if&lt;/code&gt; takes a boolean condition and two types as arguments. If the condition is true, then &lt;code&gt;eval_if&lt;/code&gt; inherits the first type; otherwise it inherits the second type. As with &lt;code&gt;if_&lt;/code&gt;, there are two versions of &lt;code&gt;eval_if&lt;/code&gt;: one that takes a &lt;code&gt;bool&lt;/code&gt; condition, and one that takes a class with a nested &lt;code&gt;value&lt;/code&gt;.&lt;/p&gt;
&lt;pre class="brush: cpp"&gt;template&amp;lt;bool c, class F1, class F2&amp;gt;
struct eval_if_c : if_c&amp;lt;c,F1,F2&amp;gt;::type { };

template&amp;lt;class C, class F1, class F2&amp;gt;
struct eval_if : if_&amp;lt;C,F1,F2&amp;gt;::type { };&lt;/pre&gt;
&lt;p&gt;Now we are ready to implement &lt;code&gt;and_&lt;/code&gt;.&lt;/p&gt;
&lt;pre class="brush: cpp"&gt;template&amp;lt;class... F&amp;gt;
struct and_;

template&amp;lt;class F1, class F2, class... F&amp;gt;
struct and_&amp;lt;F1,F2,F...&amp;gt; : eval_if&amp;lt;F1,and_&amp;lt;F2,F...&amp;gt;,false_&amp;gt;::type { };

template&amp;lt;class F1, class F2&amp;gt;
struct and_&amp;lt;F1,F2&amp;gt; : eval_if&amp;lt;F1,eval_if&amp;lt;F2,true_,false_&amp;gt;,false_&amp;gt;::type { };&lt;/pre&gt;
&lt;p&gt;Let's examine the two specializations of &lt;code&gt;and_&lt;/code&gt; one at a time. The critical bit of code in the first is &lt;code&gt;eval_if&amp;lt;F1,and_&amp;lt;F2,F...&amp;gt;,false_&amp;gt;::type&lt;/code&gt;. This names a type. If &lt;code&gt;F1&lt;/code&gt; is false, then the type is &lt;code&gt;false_&lt;/code&gt;; otherwise it is &lt;code&gt;and_&amp;lt;F2,F...&amp;gt;&lt;/code&gt;. Thus if the first argument to &lt;code&gt;and_&lt;/code&gt; is false, then it inherits &lt;code&gt;false_&lt;/code&gt;; otherwise it recurses and inherits &lt;code&gt;and_&amp;lt;F2,F...&amp;gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The second specialization ends the recursing of the first when &lt;code&gt;and_&lt;/code&gt; is evaluating exactly two arguments. If the first argument to &lt;code&gt;and_&lt;/code&gt; is false, then this specialization inherits &lt;code&gt;false_&lt;/code&gt;; otherwise it inherits &lt;code&gt;eval_if&amp;lt;F2,true_,false_&amp;gt;&lt;/code&gt;, which in turn inherits either either &lt;code&gt;true_&lt;/code&gt; or &lt;code&gt;false_&lt;/code&gt; depending on &lt;code&gt;F2&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The implementation of logical &lt;code&gt;or&lt;/code&gt; is similar.&lt;/p&gt;
&lt;pre class="brush: cpp"&gt;template&amp;lt;class... F&amp;gt;
struct or_;

template&amp;lt;class F1, class F2, class... F&amp;gt;
struct or_&amp;lt;F1,F2,F...&amp;gt; : eval_if&amp;lt;F1,true_,or_&amp;lt;F2,F...&amp;gt;&amp;gt;::type { };

template&amp;lt;class F1, class F2&amp;gt;
struct or_&amp;lt;F1,F2&amp;gt; : eval_if&amp;lt;F1,true_,eval_if&amp;lt;F2,true_,false_&amp;gt;&amp;gt;::type { };&lt;/pre&gt;
&lt;p&gt;The class names used here are borrowed from the the Boost Metaprogramming Library, which inspired this post. The implementation is my own.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5683827728680597230-3268363963469139284?l=stillmoreperfect.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://stillmoreperfect.blogspot.com/feeds/3268363963469139284/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://stillmoreperfect.blogspot.com/2010/03/template-metaprogramming-compile-time.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5683827728680597230/posts/default/3268363963469139284'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5683827728680597230/posts/default/3268363963469139284'/><link rel='alternate' type='text/html' href='http://stillmoreperfect.blogspot.com/2010/03/template-metaprogramming-compile-time.html' title='Compile-time logic'/><author><name>dmitchell</name><uri>http://www.blogger.com/profile/04848881387975069067</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
