Changes between Version 1 and Version 2 of SoC2017


Ignore:
Timestamp:
Jan 25, 2017, 8:31:04 AM (6 years ago)
Author:
Niall Douglas
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • SoC2017

    v1 v2  
    145145// Easy: generates no runtime code, this is as if literal "apple"
    146146constexpr const char *what_is_5 = cmap[5];
     147// Easy: generates no runtime code, this is as if literal "apple"
     148const char *what_is_0 = std::get<0>(cmap);
    147149
    148150// Challenging: needs to only generate code loading immediately from a memory location.
     
    154156 executed by the compiler: the compiler only ''has'' to constexpr execute when all the inputs are constant
    155157 expressions '''and''' all the outputs will be used in constant expressions, and it only ''may'' constexpr execute
    156  when all the inputs are constant expressions. Indeed empirical testing of clang 3.7 and VS2015 found both currently
    157  do '''not''' constexpr execute when the result is not stored constexpr, so in a naive implementation the last
    158  statement '''does''' generate a runtime lookup on current compiler technology despite the constant expression inputs.
    159  If you'd like to see more detail about this specific problem, have a look at the assembler generated for a toy
    160  static_map implementation at https://goo.gl/eO7ooa.
     158 when all the inputs are constant expressions. The outcome therefore depends entirely on which compiler version
     159 you are using and a fair bit of luck - sometimes GCC but not clang will work, sometimes it's the opposite. And
     160 Visual Studio is always a surprise!
    161161
    1621622. To implement a `static_map` class which runs on at least two major C++ compilers.
     
    180180
    181181==== Programming competency test ====
    182 Write a function which uses one of the compile time string hashing techniques at https://stackoverflow.com/questions/2111667/compile-time-string-hashing
    183 (or a constexpr hash function of your preference) to generate a constexpr `std::array<unsigned>` of its input strings. The function ought to have the following
    184 prototype:
    185 
    186 {{{
    187 #!c++
    188 template<class... Strings> constexpr std::array<unsigned, sizeof...(Strings)> hash_strings(Strings&&... strings);
    189 
    190 // Examples of usage
    191 constexpr std::array<unsigned, 0> string_hashes0 = hash_strings();
    192 constexpr std::array<unsigned, 2> string_hashes2 = hash_strings("niall", "douglas");
    193 constexpr std::array<unsigned, 4> string_hashes4 = hash_strings("google", "summer", "of", "code");
    194 // This should fail elegantly and usefully ...
    195 //constexpr std::array<unsigned, 0> string_hashes_fail = hash_strings(5);
    196 }}}
    197 
    198 In your submission you should:
    199 
    200 1. Explain why you chose the constexpr string hash function you did and the strengths of your choice over other choices.
    201 2. Test that the output is identical whether executed by the compiler at compile time, or at runtime.
    202 3. '''Prove''' that the compile time constexpr implementation generates no runtime code whatsoever.
     1821. Persuade the toy static_map implementation at https://goo.gl/eO7ooa to generate the same minimum output
     183under -O3 optimisation with GCC as it does with clang. Extra bonus points if you can persuade VS2017 to
     184do the same. You may use C++ 17 features if you find that helps.
    203185
    204186Submission of the programming test should be via copying and pasting the code you wrote into the end of the proposal you submit to Google Melange.