The main() Course -- Adi Shavit

A fanciful little post about li’l old main().

The main() Course

by Adi Shavit

From the article:

The function main() is a normal program’s entry point.

The shortest conforming C++ executable program is: int main(){}

CppCon 2016: What We've Learned From the C++ Community--Robert Irving & Jason Turner

Have you registered for CppCon 2017 in September? Don’t delay – Registration is open now.

While we wait for this year’s event, we’re featuring videos of some of the 100+ talks from CppCon 2016 for you to enjoy. Here is today’s feature:

What We've Learned From the C++ Community

by Robert Irving & Jason Turner

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

For over a year and a half Rob and Jason have been engaging with the speakers, library authors, bloggers and luminaries of the C++ community for their podcast, CppCast. In this talk they'll share the most interesting tools, insights and lessons they have learned from interviewing and interacting with the C++ community

CppCast Episode 116: Volta and Cuda C++ with Olivier Giroux

Episode 116 of CppCast the only podcast for C++ developers by C++ developers. In this episode Rob and Jason are joined by Olivier Giroux from NVidia to talk about programming for the Volta GPU.

CppCast Episode 116: Volta and Cuda C++ with Olivier Giroux

by Rob Irving and Jason Turner

About the interviewee:

Olivier Giroux has worked on eight GPU and four SM architecture generations released by NVIDIA. Lately, he works to clarify the forms and semantics of valid GPU programs, present and future. He was the programming model lead for the new NVIDIA Volta architecture. He is a member of WG21, the ISO C++ committee, and is a passionate contributor to C++'s forward progress guarantees and memory model.

Visual Studio extensions for C++ developers in Visual Studio 2017--Adam Welch

Several Visual Studio extensions that can make your life better as a C++ developer:

Visual Studio extensions for C++ developers in Visual Studio 2017

by Adam Welch

From the article:

In this blogpost we want to highlight several Visual Studio extensions that can make your life better as a C++ developer if you’re using Visual Studio 2017 or considering upgrading...

Quick Q: Array Initialisation Compile Time - Constexpr Sequence

Quick A: Use integer_sequence with a helper function.

Recently on SO:

Array Initialisation Compile Time - Constexpr Sequence

1) How to implement that kind of integer_sequence?

template <std::size_t... Is>
constexpr auto make_sequence_impl(std::index_sequence<Is...>)
{
    return std::index_sequence<generate_ith_number(Is)...>{};
}

template <std::size_t N>
constexpr auto make_sequence()
{
    return make_sequence_impl(std::make_index_sequence<N>{});
}

2) Is it possible to build an std::array from that integer_sequence at compile time?

template <std::size_t... Is>
constexpr auto make_array_from_sequence_impl(std::index_sequence<Is...>)
{
    return std::array<std::size_t, sizeof...(Is)>{Is...};
}

template <typename Seq>
constexpr auto make_array_from_sequence(Seq)
{
    return make_array_from_sequence_impl(Seq{});
}

Usage:

int main()
{
    constexpr auto arr = make_array_from_sequence(make_sequence<6>());
    static_assert(arr[0] == 0);
    static_assert(arr[1] == 1);
    static_assert(arr[2] == 2);
    static_assert(arr[3] == 4);
    static_assert(arr[4] == 5);
    static_assert(arr[5] == 7);
}

Quick Q: vector of vector does not convert brace encloser list

Quick A: The constructor needs an extra {} pair.

Recently on SO:

vector does not convert brace encloser list

You have two options:

  1. add a constructor taking std::initializer_list<std::initializer_list<T>>
  2. eclose the init expression with another set of {} i.e.
Matrix<double> a{{

    { 17,    24,    1},

    { 23,    5,     7 },

    {  4,     6,    13 }

}};

Ok, I'll try a little explanation of what is going on here:

If there is no constructor taking a std::initializer_list then the outermost {} are always opening and closing the constructor call if you will, and not part of what you actually pass to the constructor.

Matrix<double> a{ {1, 2}, {3, 4} };
                ^ ^~~~~~~~~~~~~~ ^
                |  2 parameters  |
                |                |
                |                |
            opening            closing

As you can see this is taken as a constructor with 2 parameters, in this case 2 initializer_lists.

This is why you need another set of {}:

Matrix<double> a{ {{1, 2}, {3, 4}} };
                ^ ^~~~~~~~~~~~~~~~ ^
                |  1 parameter     |
                |                  |
                |                  |
            opening            closing

In order for the outermost {} to be considered an initializer_list then the constructor needs to have an overload taking a initializer_list. That is what is happening in the std::vector case.