Senpai code question

Code, algorithms, languages, construction...
Post Reply
rtr
Posts: 8
Joined: Thu Apr 03, 2014 2:39 am

Senpai code question

Post by rtr » Fri Apr 04, 2014 6:06 am

What is meant here?

Code: Select all

namespace gen_sort {

// HACK: outside of List class because of C++ "static const" limitations :(

Octopus

Re: Senpai code question

Post by Octopus » Fri Apr 04, 2014 8:43 am

Well, I don't know, what exactly you are pointing to. In early
C++ compilers it wasn't possible to freely use static constants.
Instead then enum constructs has been used or otherwise also
ugly #define CONSTANT definitions, known from elder C days.

User923005
Posts: 616
Joined: Thu May 19, 2011 1:35 am

Re: Senpai code question

Post by User923005 » Fri Apr 04, 2014 10:48 am

Here are some things that point to oddities with static const:
http://www.cplusplus.com/forum/general/103814/
http://codewrangler.home.comcast.net/~c ... const.html

Code: Select all

#include <string>
// It seems unnatural that:
class THHGTTG {
private:
 static const int A2tUQofLtUaE = 42; // OK
 static const std::string fortyTwo("42"); //NOT OK
};
For aggregate elements, you will see initializations in places that seem peculiar. IMO-YMMV.

rtr
Posts: 8
Joined: Thu Apr 03, 2014 2:39 am

Re: Senpai code question

Post by rtr » Fri Apr 04, 2014 5:56 pm

Is constexpr any better, or still awkward?

Code: Select all

#include <iostream>
#include <array>

struct List {
    enum Inst {
        GEN_EVASION,
        GEN_TRANS,
        GEN_TACTICAL,
        GEN_KILLER,
        GEN_CHECK,
        GEN_PAWN,
        GEN_QUIET,
        GEN_BAD,
        GEN_END,
        POST_MOVE,
        POST_MOVE_SEE,
        POST_KILLER,
        POST_KILLER_SEE,
        POST_BAD,
    };

    static constexpr std::array<Inst,11> Prog_Main = {{ GEN_TRANS, POST_KILLER, GEN_TACTICAL, POST_MOVE_SEE, GEN_KILLER, POST_KILLER_SEE, GEN_QUIET, POST_MOVE_SEE, GEN_BAD, POST_BAD, GEN_END }};
    static constexpr std::array<Inst,9> Prog_QS_Root = {{ GEN_TRANS, POST_KILLER, GEN_TACTICAL, POST_MOVE, GEN_CHECK, POST_KILLER, GEN_PAWN, POST_MOVE, GEN_END }};
    static constexpr std::array<Inst,5> Prog_QS = {{ GEN_TRANS, POST_KILLER, GEN_TACTICAL, POST_MOVE, GEN_END }};
    static constexpr std::array<Inst,5> Prog_Evasion = {{ GEN_EVASION, POST_MOVE_SEE, GEN_BAD, POST_BAD, GEN_END }};
    
};

constexpr std::array<List::Inst,11> List::Prog_Main;
constexpr std::array<List::Inst,9> List::Prog_QS_Root;
constexpr std::array<List::Inst,5> List::Prog_QS;
constexpr std::array<List::Inst,5> List::Prog_Evasion;

int main (int argc, char * argv[]) {
    for (auto x : List::Prog_Main)
        std::cout << x << std::endl;
    return 0;
}

User923005
Posts: 616
Joined: Thu May 19, 2011 1:35 am

Re: Senpai code question

Post by User923005 » Fri Apr 04, 2014 7:52 pm

Maybe it is a step in the right direction. It has some quirks too, but it solves some problems. Requires C++11 or C++14. In C++14 it changes meaning a bit. See:
http://en.cppreference.com/w/cpp/language/constexpr
http://akrzemi1.wordpress.com/2013/06/2 ... not-const/

Here is a nice link about it:
http://www.cprogramming.com/c++11/c++11 ... texpr.html

Post Reply