• a particular "explicit specialization" case

    From phpete48@googlemail.com@21:1/5 to All on Sun Mar 20 16:06:04 2016
    { edited by mod to shorten lines to ~70 characters. -mod }

    I'm using g++ and Ubuntu 14.04. The case is the code for an arbitrary
    hash function. The originators evidently intended it as an explicit specialization template. However, it would not compile as is. So, I
    commented out some of the
    code and then it compiled and ran correctly.

    While I understand the basic idea of explicit specialization, I'm
    unclear about the syntax. Could someone please explain how to modify
    this code, so it resembles the original, and compiles, and runs.
    Also, could you inform us why they set it up in this manner.

    Thanks,

    ------------------------------------------------------------------------
    ---

    //template <> struct myhash{};
    //
    //template <> struct myhash<std::string>
    class myhash {
    public:
    size_t operator()(const std::string &to_hash) const {
    const char *in = to_hash.c_str();
    size_t out=0;
    while(*in != '\0') {
    out*= 53; //just a prime number
    out+= *in;
    ++in;
    }
    return out;
    }
    };


    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From =?ISO-8859-1?Q?=D6=F6_Tiib?=@21:1/5 to phpe...@googlemail.com on Mon Mar 21 07:27:08 2016
    On Sunday, 20 March 2016 23:10:16 UTC+2, phpe...@googlemail.com wrote:

    I'm using g++ and Ubuntu 14.04. The case is the code for an arbitrary
    hash function. The originators evidently intended it as an explicit specialization template. However, it would not compile as is. So, I commented out some of the
    code and then it compiled and ran correctly.

    That is voodoo programming anti-pattern. Don't do it. Software is not
    magic incantations but something we should be able to understand. Read
    the compiler diagnostics. These are also not "won't compile" in Dark
    Tongue of Mordor, but meant as helpful text about what went wrong that
    we are supposed to understand and to write here to others when we don't understand.

    While I understand the basic idea of explicit specialization, I'm
    unclear about the syntax. Could someone please explain how to modify
    this code, so it resembles the original, and compiles, and runs.

    The original that you specialize should be something like:

    template<typename Key> struct myhash;

    That looked like simple syntax error in your code with 'typename Key'
    missing. All compilers that I tried complain about it quite clearly.
    Then you can specialize it as:

    template <> struct myhash<YourStuff> {/* ... */};

    That looked correct in your code.

    Also, could you inform us why they set it up in this manner.

    Generally such a template is used as default doer of some
    subtask of some bigger template. For example there are numerous
    containers that use hash for optimizing the work and what hash
    exactly can be configured with template arguments.

    However we can simply type 'std::unordered_map<int, std::string>'
    when we are fine that rest of the 'unordered_map' arguments are
    default 'std::hash<int>', 'std::equal_to<int>' and 'std::allocator<std::pair<const int,std::string>>'.

    So if "originators" made some templates that use "myhash"
    as default somewhere and you need to use their templates then
    most convenient for you is likely to specialize that "myhash".




    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)