Recent Updates Toggle Comment Threads | Keyboard Shortcuts

  • Alex Sinyakov 13:28 on April 7, 2012 Permalink | Reply
    Tags: , factorial   

    calculation of factorial using different facilities of C++ language 

    I’ve collected all known techniques to calculate factorial using C++. Enjoy :)

    1. using free function
    int f(int n)
    {
    return n <= 1 ? 1 : n * f(n – 1);
    }

    int x = f(42);

    2. using lambdas
    std::function f = [&f](int n) -> int
    {
    return n <= 1 ? 1 : n * f(n – 1);
    };

    int x = f(42);

    3. using constexpr
    constexpr int f(int n)
    {
    return n <= 1 ? 1 : n * f(n – 1);
    }

    const int x = f(42);

    4. using templates

    template
    struct factorial
    {
    enum { value = N * factorial::val; };
    };

    template
    struct factorial
    {
    enum { value = 0; }
    };

    const int x = Factorial::value;

     
  • Alex Sinyakov 23:25 on March 13, 2012 Permalink | Reply
    Tags:   

    The 2012-02 post-Kona mailing is available 

    You can check it out here: http://www.open-std.org/jtc1/sc22/wg21/

    The main anticipated, imho, features are:

    static if – it will simplify a huge amount of meta code. 

    range – we 100% need ranges. I think everybody is tired of writing begin/end, even if these pair used only in 5% of code :)

    Filesystem Library for C++11 – as you may know, msvc2011 already have <filesystem> header.

    Modules in C++ – it is the major feature of C++. All benefits are listed in proposal. But, for me, the main advantage is get rid off pimple idiom.

    boost.asio – please, make it part of standard library. we want platform-independent network library.

     

    I know many huge companies that don’t allow using of boost(or any other 3rd party libraries) in their products. I’m tired of this. Please, make it part of standard.

     
  • Alex Sinyakov 00:10 on March 11, 2012 Permalink | Reply
    Tags:   

    Tags technique 

    Suppose you’ll need to write some class mat4, that represent transformation 4×4 matrix. This matrix is widely used in graphics. The first question that you need to address as developer is “what default constructor of mat4 should look like?” Well, of course, you can set all elements of matrix to zero. But this kind of matrix is not usable in graphics application. The more common matrix is identity matrix. On the other hand, you want to aware of unnecessary initialization of matrix. Here is the technique that help us to solve all these designing questions.
    Let’s introduce some tags
    in *.h file:

    struct mat4_identity{ static mat4_identity tag; };
    struct mat4_zero{ static mat4_zero tag; };
    struct mat4_unitialized{ static mat4_unitialized tag; };
    _Winnie C++ Colorizer

    in *.cpp file:

    mat4_identity mat4_identity::tag;
    mat4_zero mat4_zero::tag;
    mat4_unitialized mat4_unitialized::tag;
    _Winnie C++ Colorizer

    And delete our default constructor:

    mat4() = delete;
    _Winnie C++ Colorizer

    Now implement three constructors:

    mat4::mat4(mat4_identity)
    {
    for(size_t i = 0; i &lt; mat4::arity; ++i)
    for(size_t j = 0; j &lt; mat4::arity; ++j)
    {
    data[i][j] = i == j ? scalar(1) : scalar(0);
    }
    }

    mat4::mat4(mat4_zero)
    {
    for(size_t i = 0; i &lt; mat4::arity; ++i)
    for(size_t j = 0; j &lt; mat4::arity; ++j)
    data[i][j] = scalar(0);
    }

    mat4::mat4(mat4_unitialized)
    {
    //yep, it is your decision
    }

    _Winnie C++ Colorizer

    Now we can perform calculations are very quickly. Without additional overhead to matrix initialization.

    mat4 mat4::operator+(const mat4&amp; rhs)
    {
    mat4 result(mat4_unitialized::tag);

    for(size_t i = 0; i &lt; arity; ++i)
    for(size_t j = 0; j &lt; arity; ++j)
    result.data[i][j] = data[i][j] + rhs.data[i][j];

    return result;
    }

    _Winnie C++ Colorizer
     
  • Alex Sinyakov 20:49 on March 10, 2012 Permalink | Reply
    Tags:   

    lambda in std::bind, msvc 2010 

    Unfortunately, msvc2010 doesn’t support lambdas as parameter for std::bind. For example,

    you cannot write something like this:

    std::bind([](int x){ return x; },42)();

    The problem is that bind powered by result_of, and vc10 uses old way to handle result of the expression.

    In vc11 it is fixed. Internally result_of uses decltype.

    As always, you can write functor instead of lambda and pass it to bind function:
    class T
    {
    public:
    int operator()(int x)
    {
    return x;
    }
    };

    T f;
    std::bind(f,42);

     
c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel
Follow

Get every new post delivered to your Inbox.