f



a particular "explicit specialization" case

{ 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! ]

0
phpete48
3/20/2016 4:06:04 PM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

1 Replies
412 Views

Similar Articles

[PageSpeed] 39

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! ]

0
ISO
3/21/2016 7:27:08 AM
Reply:

Similar Artilces:

REQ HELP: Upper Case "C" replaced Lower Case "c" in Table
All the lower case "c" in my database table are now upper case "C" starting in January 2004. All prior data is fine. I just noticed this after installing a Microsoft update patch a couple of days ago. Any ideas what happened, or how to fix? I thought I could change every letter C to lower case, and then change every c that starts a word to an upper case C, but I don't know how to do the second step. Thanks, MHenry It also replaced the p with a P in addition to the c with a C. Hmmm... PC Could this be a virus? Is there some command someone could have accidentally...

["a", "b", "c", "d"] to "a, b, c, d"?
I want to process each element of an array, but the last element should be handled special. Here is an example: def p_ary(ary) str = "" ary.each do |elem| str << elem << ", " end str.chomp!(", ") str end so p_ary(["a", "f", "x", "test"]) produces "a, f, x, test". The code works, but isn't there an easier and more general way for this behaviour? martinus On Tue, 06 Apr 2004 04:23:22 -0700, Martin wrote: > I want to process each element of an array, but the last ele...

"if" as a special case of "cond"
hi First, I must say it's very hard for me to learn scheme (or lisp), again. I've just finished a project translating a bunch of c codes to verilog codes. It's getting very hard for me to think abstractingly after all these years of programming in dealing with low level details. Now I'm trying to get back to higher level of thinking. It's very hard! I remembered the days when I used to write autolisp codes, what fun... Anyway, I'm reading MIT's textbook on scheme. It was talking about "if" as a special case of "cond". Now, after thinkg a little...

What does "Standard C", "K&R C" , "ANSI C" mean?
I am just wondering what the following terms usually mean: 1) "Standard C" 2) "K&R C" 3) "ANSI C" I am pretty sure "ANSI C" usually refers to the C89 standard, but what about the other two? What is the "saying" for C99 standard? Thank you On 17 Jan 2005 21:26:42 -0800, "Luke Wu" <LookSkywalker@gmail.com> wrote in comp.lang.c: > I am just wondering what the following terms usually mean: > > 1) "Standard C" The current version of the C language standard. This is now known as "ISO/IEC 9899:19...

why "::", not "."
Why does the method of modules use a dot, and the constants a double colon? e.g. Math::PI and Math.cos -- Posted via http://www.ruby-forum.com/. On Oct 26, 2010, at 01:48 , Oleg Igor wrote: > Why does the method of modules use a dot, and the constants a double > colon? > e.g. > Math::PI and Math.cos For the same reason why inner-classes/modules use double colon, because = they're constants and that's how you look up via constant namespace. Math::PI and ActiveRecord::Base are the same type of lookup... it is = just that Base is a module and PI is a float....

"out" and "in out"
Hi i found the following explaination: In Ada, "in" parameters are similar to C++ const parameters. They are effectively read-only within the scope of the called subprogram. Ada "in out" parameters have a reliable initial value (that passed in from the calling subprogram) and may be modified within the scope of the called procedure. Ada "out" parameters have no reliable initial value, but are expected to be assigned a value within the called procedure. What does "have no reliable initial value" mean when considering the "out" parameter? By c...

about "++" and "--"
why this program snippet display "8,7,7,8,-7,-8" the program is: main() { int i=8; printf("%d\n%d\n%d\n%d\n%d\n%d\n",++i,--i,i++,i--,-i++,-i--); } > why this program snippet display "8,7,7,8,-7,-8" Ask your compiler-vendor because this result is IMHO implementation-defined. Check this out: http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.15 http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.16 Regards, Irina Marudina fxc123@gmail.com wrote: > why this program snippet display "8,7,7,8,-7,-8&q...

Urgent need """""""""""INFORMATICA DEVELOPER"""""""""""""
Hello Partners, How are you ? Please find the requirements below. Title: Database/ETL Developer Duration: 6 months Location: NY Exp: 7+ Locals preferred Database/ETL requirements (Mandatory) Candidate must have worked with financial instruments, preferably Mutual Funds but, Equities are also ok. PL/SQL - packages, Stored procs, Functions, Aggregate functions, Pipelined Functions Informatica 8.6 - especially complex mappings, complex maplets, complex workflows, transformations Oracle 10g/11g Unix/Linux shell scripting Database/ETL requirements (Optional) Data warehousing experience Threading and job concepts in 10g/11g Cost based Optimizer concepts in 10g/11g Must : Experience with XML files and partitioning concepts in Oracle, Collections, Material Views Note : No phone calls please. : send Resumes to karthik@bhaninfo.com Thanks & Regards Karthik BhanInfo karthik@bhaninfo.com ...

How to "break" a "case-when" ?
SGksIGlzIG5vdCBwb3NzaWJsZSB0byB0ZXJtaW5hdGUgdGhlIGJvZHkgb2YgYSAid2hlbiIgc3Rh bWVudD8KCkkgbWVhbjoKCiAgY2FzZSBudW0KICB3aGVuIDE6CiAgICBwdXRzICJJcyAxICEhISEi CiAgICBicmVhawogICAgcHV0cyAiRG9uJ3Qgd3JpdGUgaXQiCiAgZW5kCgogIHB1dHMgIldyaXRl IGl0IgoKYnV0IHVuZm9ydHVuYXRlbGx5IHRoZSAiYnJlYWsiIGdpdmVzIGFuIGVycm9yLgoKVGhh bmtzIGZvciBhbnkgc3VnZ2VzdGlvbi4KCi0tIApJw7Fha2kgQmF6IENhc3RpbGxvCjxpYmNAYWxp YXgubmV0Pgo= RnJvbTogScOxYWtpIEJheiBDYXN0aWxsbyBbbWFpbHRvOmliY0BhbGlheC5uZXRdIA0KIyBIaSwg aXMgbm90IHBvc3NpYmxlIHRvIHRlcm1pbmF0ZSB0aGUgYm9keSBvZiBhICJ3aGVuIiBzdGFtZW50 Pw0KIyAgIGNhc2UgbnVtDQojICAgd2hlbiAxOg0KIyAgIC...

"In" "Out" and "Trash"
I just bought a new computer and I re-installed Eudora Light on my new computer. But when I open Eudora, the "In", "Out" and "Trash" links are not on the left side of the screen the way they were on my old computer. How can I get these links back on the left side of the screen? Thank you. On 25 Mar 2007 09:49:22 -0700, "abx" <abfunex@yahoo.com> wrote: >I just bought a new computer and I re-installed Eudora Light on my new >computer. But when I open Eudora, the "In", "Out" and "Trash" links >are ...

C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression"
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15) says under 6.4.2(2) [see also 5.19]: case constant-expression : I propose that the case expression of the switch statement be changed from "integral constant-expression" to "integral expression". This opens up many new possibilities since then also function calls would be permitted in the case expression. The old case case would continue to function since it is a subset of the new case case. Example usage: //... int f() { //... return BLA1; } int g() { //... return BLA2; } int h() { //... return BLA3; } int y, x = f(); switch (x) { case 123 : y = g(); break; case g() : y = 456; break; // using new case feature, ie. func-call case h() : y = 789; break; // ditto default : y = -1; break; } On October 23, 2008 14:17, in comp.lang.c, Adem (for-usenet-5c@alicewho.com) wrote: > C/C++ language proposal: > Change the 'case expression' from "integral constant-expression" to > "integral expression" > > The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15) > says under 6.4.2(2) [see also 5.19]: [snip] Sorry, but your proposal is off-topic for the comp.lang.c newsgroup. I suspect that it is also off-topic for comp.lang.c++....

dolby "B", "C"
I've got some old cassettes with unique and original recordings that I'de like to move onto computer. They were recorded with Dolby B. Some were recorded with dolby C. I'de like to transfer the tapes to computer and then "decode" the dolby on the wav files after. My understanding is that since dolby is an analog system, it should be simple to emulate it. I already have a feeling that an expander and a low pass filter (whose strength would follow the loudness contour of the audio) might be the way to go. Any thoughts? Just curious, why do you want to do it this w...

Urgent JAVA Requirement in """"""NEW YORK"""""""""
Hello Partners, How are you ? Please find the requirement below. Location : NY Duration : 8 mnths Rate :Open Job description: Java/J2EE Web Service Developer =B7 (4+ years of application development experience in Java/J2EE and Web service technologies. =B7 Experience with spring & Hibernate. =B7 Experience with J2EE Application Server (preferably Web logic). =B7 Preferable Aqua logic DSP Experience =B7 Preferable Sonic ESB Composite Service experience Experience working in UNIX environment. Good working knowledge of Oracle RDBMS and PL/SQL) Note : Please send your updated resume along with contact no to Karthik@bhaninfo.com. Thanks & Regards Karthik BhanInfo karthik@bhaninfo.com # 1572, 7 th Floor Suites, 1 Blue Hill Plaza Pearl River,NY-10965 www.bhaninfo.com ...

Gary Sokolich """"""
"""""""""" http://www.manta.com/c/mmlq5dm/w-gary-sokolich W Gary Sokolich 801 Kings Road Newport Beach, CA 92663-5715 (949) 650-5379 http://www.tbpe.state.tx.us/da/da022808.htm TEXAS BOARD OF PROFESSIONAL ENGINEERS February 28, 2008 Board Meeting Disciplinary Actions W. Gary Sokolich , Newport Beach, California �V File B-29812 - It was alleged that Dr. Sokolich unlawfully offered or attempted to practice engineering in Texas (...) Dr. Sokolich chose to end the proceedings by signing a Consent Order that was accepted by ...

Question about "sprintf" "@" "do for"
Hello, this works: A1=3D1 A2=3D2 A3=3D3 i=3D1 vari=3Dsprintf("A%.f",i) print vari,"=3D",@vari i=3Di+1 vari=3Dsprintf("A%.f",i) print vari,"=3D",@vari i=3Di+1 vari=3Dsprintf("A%.f",i) print vari,"=3D",@vari do for [i=3D1:3]{ vari=3Dsprintf("A%.f",i) print vari } But I want to have "print vari,"=3D",@vari" in the loop. But it dosen't=20 work. Why can't I use "print vari,"=3D",@vari" in the loop? Is there a=20 solution for? J=C3=B6rg Jörg ...

puts "\\".gsub("\\", "\\\\")
Hello, I have a mini-ruby quiz. Guess what this line of code writes to the console, then try it for yourself: puts "\\".gsub("\\", "\\\\") Why is that so? Martin From: martinus [mailto:martin.ankerl@gmail.com]=20 # Hello, I have a mini-ruby quiz. Guess what this line of code writes to # the console, then try it for yourself: # puts "\\".gsub("\\", "\\\\") puts "\\".gsub("\\", "\\\\") \ #=3D> nil # Why is that so? faq. escaping the escape in sub/gsub. search the archives. maybe you want somethin...

how make /%([0-f]{2})/ -> sprintf( "%c", "\x" "\\1" ) problem : var = "\x27" # work var = 27 ; var = "\x" var ; # doesnt work, for sprintf %c
.... On 27.10.2014 18:32, {xmb} wrote: > ... > What a stupid question! Read http://www.catb.org/esr/faqs/smart-questions.html and Learn or Leave thank you _not_ for your useless text. if u want to help do so, u didnt. On Tue, 28 Oct 2014 07:42:00 -0700, {xmb} wrote: > thank you _not_ for your useless text. if u want to help do so, u didnt. Your question was unintelligible. The people in this group are very willing to help, in my experience. Your question just did not show the proper attitude or mental state to justify spending time answering. ...

"A" is not equal "A"?
I was trying to convert String Array to String A through cycle and than compare the result with String B but eventhough results were the same it returned false. I'am working in BlueJ 3.1.0 Code is: public boolean Method () { String[] Example = {"a","b","c"}; String A = ""; String B = "abc" for (int i = 0; i<3 ; i++) { A = A + Example[i]; } if (A == B) { return true; } } never got true... I appreciate any suggestions how to solve this Den 08.04.2014 10:06, skrev 245dav@gmail.com:...

Changing case "message" to "MESSAGE"
Working my way through Koenig & Moo "Accelerated C++" and using Borlang Builder 5 (but that is probably not relevant) I reached exercise 1-1 and typed in #include <vcl.h> #pragma hdrstop #include <iostream> #include <string> int main(int argc, char*argv[]} { const std::string hello = "Hello" // This by itself compiled const std::string message = "Hello" // and other stuff which I have //left out here. system("PAUSE"); return 0; } But I left caps lock on, and by mistake typed MESSAGE, an...

want a "here doc" equivalent in "c"
I want to embed a bash script inside the c program. How could you do the below bash snippet in "c"?: cat > /tmp/foo <<\__EOD_ all kinds of nasty stuff in here including single and double quotes and bashslashes .. (note that the here doc delimiter is preceded by a "\" __EOD_ -- regards, Tom Rodman pls run for my address: perl -e 'print unpack("u", "1\:6UP\,\$\!T\<F\]D\;6\%N\+F\-O\;0H\`");' On Wed, 03 Sep 2003 15:14:28 -0500 Tom Rodman <Use-Author-Address-Header@[127.1]> wrote: > I want to embed a bash script in...

why have both "." and "->" ?
I used to remember why c++ needed both ? Could somebody help me here ? For example class A{ f(); }; A* aa; You could do either "aa->f()" or "(*aa).f()". So why does C++ need both operators. Raj "raj" <rajkumar@hotmail.com> schreef in bericht news:d7fee6d0.0406230748.694b966b@posting.google.com... > I used to remember why c++ needed both ? > Could somebody help me here ? > > For example > > class A{ > f(); > }; > > A* aa; > > You could do either "aa->f()" or "(*aa).f()". So why does C++...

AC97 Mixer: What is "C" and "D"
I got a program called "AC97 Mixer" installed, and on the control panel, under "3D" there are sliders for "C" and "D". What do these sliders alter? Certainly I get a fuller sound by having "3D" activated. TIA. Rich. ...

Re: "out" and "in out"
"no reliable initial value" means, you cannot rely on the value, but it may have a value. This depends on the parameter passing mechanism, which is _not_ related to the parameter mode (contrary to what many people think). So the parameter mode is there (nearly) solely for the information of the reader. The parameter passing mechanism for all kinds of parameters is defined in the RM. There are parameters passed by copy (in and out), by reference; for some it is explicitly left undefined. In your case, the passing mechanism is by reference, so you get what you get. But don't rely...

A "computer consultant" who "specializes in Macs"!!
Picked off CSMS. Like Nash said; "The Dunning-Kruger effect". Jack-off of all trades, inept at every one. sctvguy1 Jun 11 Running Snow Leopard and am satisfied with it. Now, this evening, I have been getting little nag screens saying that Yosemite is available for download and to upgrade. Is anyone else getting these screens who are not running 10.10? Alan Baker Jun 11 If you go into the App Store app's Update tab, you can right click on the Yosemite update suggestion and make it stop. David Empson Jun 11 That method only works for 10.8 ...

Web resources about - a particular "explicit specialization" case - comp.lang.c++.moderated

Resources last updated: 3/20/2016 9:11:40 PM