Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Scrapez
Feb 27, 2004

The Laplace Demon posted:

Explanation and code

I really appreciate this. I had gotten close to what you have there but had a slight error remaining. I've changed the array name. I agree, it was confusing as it was a remnant of a different method I'd previously tried. I should have cleaned that up prior to posting.

Your explanation really helped me understand the code so thanks a lot. I'm not sure why I couldn't get my head around this.

Adbot
ADBOT LOVES YOU

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

The Laplace Demon posted:

I'm not sure what you mean by not possible. Templated functions can be partially specialized.

You're saying that they can be specialized for specific template arguments, which is true; this feature is called explicit specialization. Partial specialization is something you can do to class templates where the specialization is itself still templated; for example:

C++ code:

template <class T, bool isPOD = std::is_pod<T>::value>
class vector { ... }; // preferred for non-POD types

template <class T>
class vector<T, true> { ... }; // preferred for POD types

But you cannot do this to function templates.

The Laplace Demon
Jul 23, 2009

"Oh dear! Oh dear! Heisenberg is a douche!"

Scrapez posted:

I really appreciate this. I had gotten close to what you have there but had a slight error remaining. I've changed the array name. I agree, it was confusing as it was a remnant of a different method I'd previously tried. I should have cleaned that up prior to posting.

Your explanation really helped me understand the code so thanks a lot. I'm not sure why I couldn't get my head around this.

I had to refer to somewhere else to get my head straight on it too. :) Like so many short and sweet algorithms, the details can be tricky.

The Laplace Demon
Jul 23, 2009

"Oh dear! Oh dear! Heisenberg is a douche!"

rjmccall posted:

You're saying that they can be specialized for specific template arguments, which is true; this feature is called explicit specialization. Partial specialization is something you can do to class templates where the specialization is itself still templated; for example:

C++ code:
template <class T, bool isPOD = std::is_pod<T>::value>
class vector { ... }; // preferred for non-POD types

template <class T>
class vector<T, true> { ... }; // preferred for POD types
But you cannot do this to function templates.

Oh, gotcha. Clever use of SFINAE (e.g. std::enable_if, etc) can work around this, but it gets extremely ugly and can be frustrating. Depending on the situation, tag dispatching can be a fairly clean way to handle this. I've also resorted to putting the implementation inside a static member function of a partially specialized class.

As for why it's not possible... :shrug: Possibly related to the whole function/template overload bonanza?

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

The Laplace Demon posted:

If you're using C++11, you can even check your bounds at compile time so you don't have those pesky out-of-bounds access issues.
C++ code:
template<size_t n>
int fart(int (&butt)[n])
{
    static_assert(n and n-1 &~ 1, "poop");
    return 2[butt];
}

What does the "and" mean in "n and n-1"? (I did try to search for this but you know, not a great word to be trying to search for)

shrughes
Oct 11, 2008

(call/cc call/cc)

Hammerite posted:

What does the "and" mean in "n and n-1"? (I did try to search for this but you know, not a great word to be trying to search for)

That is literally a keyword you can use as an alternative to && in C++, that nobody uses. See http://en.cppreference.com/w/cpp/language/operator_alternative. It has the same precedence as && (unless I'm sorely mistaken), unlike other languages (such as Perl) where it has weaker precedence.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

shrughes posted:

That is literally a keyword you can use as an alternative to && in C++, that nobody uses. See http://en.cppreference.com/w/cpp/language/operator_alternative. It has the same precedence as && (unless I'm sorely mistaken), unlike other languages (such as Perl) where it has weaker precedence.

Ah, I had no idea that you could do that in C++.

Malcolm XML
Aug 8, 2009

I always knew it would end like this.

rjmccall posted:

You're saying that they can be specialized for specific template arguments, which is true; this feature is called explicit specialization. Partial specialization is something you can do to class templates where the specialization is itself still templated; for example:

C++ code:
template <class T, bool isPOD = std::is_pod<T>::value>
class vector { ... }; // preferred for non-POD types

template <class T>
class vector<T, true> { ... }; // preferred for POD types
But you cannot do this to function templates.

C++ templates: all of the complexity of dependent types, none of the benefits (sans code specialization I guess)

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

OneEightHundred posted:

I'm more annoyed by template function partial specialization not being possible, and I'm still not sure why it isn't.
Funny you say that in response to my grumble because I ran into that at pretty much the same time as I ran into the "you can't specialize your member functions inside the class definition because gently caress you lol" message. I agree, that one is also annoying in much the same way, they're both "oh hey, here's an arbitrary restriction where the error message itself makes it very clear that the compiler totally understands what you're asking for, and it just refuses to do it."

Which is acceptable behavior in contexts like accidentally treating a pointer as a reference, where the error can be all "hey dude, here's what you meant to write" - it understands and won't do it, but at least it fixes it for you. But the template ones are like "hey dude, I see what you meant to do, but you can't haha! I don't wanna!"

The Laplace Demon
Jul 23, 2009

"Oh dear! Oh dear! Heisenberg is a douche!"

Hammerite posted:

Ah, I had no idea that you could do that in C++.

To expand, it's an "identifier-like operator". The rest of these lovely "features" are (ignoring new and delete):
code:
and and_eq bitand bitor compl not not_eq or or_eq xor xor_eq
I vomit a little in my mouth every time I encounter these.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

The Laplace Demon posted:

To expand, it's an "identifier-like operator". The rest of these lovely "features" are (ignoring new and delete):
code:
and and_eq bitand bitor compl not not_eq or or_eq xor xor_eq
I vomit a little in my mouth every time I encounter these.
Can they be overloaded separately from the regular operators? That could make them A. kind of useful, and B. hilariously confusing.

shrughes
Oct 11, 2008

(call/cc call/cc)
No, they're alternative names for when your character set doesn't have "&".

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

roomforthetuna posted:

Can they be overloaded separately from the regular operators? That could make them A. kind of useful, and B. hilariously confusing.

No, they're just alternate spellings, like trigraphs, in case you have a weird character set missing & or { or whatever.

e;FB

Jewel
May 2, 2009

Oh hey I wanted to show some really dumb things you can do with bitand a few days ago but didn't want to post it just for the sake of it. Now we're on the topic...

You can do this horrible nonsense because it's literally a find->replace!

C++ code:
void SomeFunction(const SomeStruct bitand a_struct)
{
    std::cout << a_struct.var << std::endl;
}
OR

C++ code:
void SomeFunction(SomeStruct* a_struct){...};

void main()
{
    SomeStruct ss;
    SomeFunction(bitand ss);
}

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
I find it weird there's replacements for the comparison operators, but none for the arithmetic ones. You can't write SomeButts mult butts_p = bitand butts; as far as I know.

xgalaxy
Jan 27, 2004
i write code

The Laplace Demon posted:

To expand, it's an "identifier-like operator". The rest of these lovely "features" are (ignoring new and delete):
code:
and and_eq bitand bitor compl not not_eq or or_eq xor xor_eq
I vomit a little in my mouth every time I encounter these.

I dont know why you say this? They are a lot more readable than the regular symbols.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Suspicious Dish posted:

I find it weird there's replacements for the comparison operators, but none for the arithmetic ones. You can't write SomeButts mult butts_p = bitand butts; as far as I know.

shrughes says that the word alternatives ("and" etc) are for cases where your character set doesn't have the necessary symbol. Unicode Explained (page 37) says that there are legacy character sets that are mostly like ASCII but substitute some symbols for others. It describes as "the invariant subset of ASCII" those characters which are the same even in international variants of ASCII. This set includes +, -, * and / and I suppose maybe it was therefore felt that adding keywords like "plus" for + would not be necessary since they should always be available?

(The "invariant subset of ASCII" also apparently includes &, but on the other hand it does not include |, so perhaps the motivating case was | and & was also given a word alternative for consistency)

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip
The arithmetic operators are part of ISO 646, so there's no need for iso646.h to define alternatives for them (and thus they aren't part of C++'s bizarre dance where everything defined in iso646.h is made a keyword and thus available in programs that don't include that header).

Vanadium
Jan 8, 2005

I define all my constructors as compl ClassName() {...} too

xgalaxy
Jan 27, 2004
i write code

Otto Skorzeny posted:

The arithmetic operators are part of ISO 646, so there's no need for iso646.h to define alternatives for them (and thus they aren't part of C++'s bizarre dance where everything defined in iso646.h is made a keyword and thus available in programs that don't include that header).

This isn't true for Visual Studio from my experience. You have to include iso646.h for the alternatives to work.

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

xgalaxy posted:

This isn't true for Visual Studio from my experience. You have to include iso646.h for the alternatives to work.

Yes, this is one of the many instances where VC++ deviates from the C++ language spec (though more understandable than the others).

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
Yeah, I was more wondering about !=. ISO646 seems to define both ! and =.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Vanadium posted:

I define all my constructors as compl ClassName() {...} too

That's a destructor, you dope.

Vanadium
Jan 8, 2005

Suspicious Dish posted:

That's a destructor, you dope.

It's hard to tell if there's no squiggly line though!

Zopotantor
Feb 24, 2013

...und ist er drin dann lassen wir ihn niemals wieder raus...

The Laplace Demon posted:

code:
and and_eq bitand bitor compl not not_eq or or_eq xor xor_eq
Whoever invented those, and digraphs, will burn in one of the sultrier corners of Hell.

The Laplace Demon
Jul 23, 2009

"Oh dear! Oh dear! Heisenberg is a douche!"

xgalaxy posted:

I dont know why you say this? They are a lot more readable than the regular symbols.

Personal preference plus hyperbole. I find them a lot less readable, as do many others it would seem. I personally like my operators to look like operators and my identifiers to look like identifiers.

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

Zopotantor posted:

Whoever invented those, and digraphs, will burn in one of the sultrier corners of Hell.

If I've read Dante right, fraud and treachery of this sort merit the 8th circle of hell, Malebolge.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

The Laplace Demon posted:

Personal preference plus hyperbole. I find them a lot less readable, as do many others it would seem. I personally like my operators to look like operators and my identifiers to look like identifiers.

It seems to be generally accepted among C(++) coders that && and || should be used rather than "and" and "or". But this is only a norm among a specific community. In Python the operators are "and" and "or", and && and || would be syntax errors. But assuming you have an editor that does syntax highlighting, there is no issue with "operators looking like identifiers". (I think that "and" and "or" are inherently more readable than the symbol versions, but I would not use them in writing C++ because they are not the way people generally expect the the language to be written.)

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Hammerite posted:

But assuming you have an editor that does syntax highlighting, there is no issue with "operators looking like identifiers".

What editor do you use that highlights the alternative tokens? They're just macros, so either it special-cases things that transitively include iso646.h and handles redefinition properly, or it's expanding macros to find those that work out to single operator tokens. Those both sound like hanging offenses.

xgalaxy
Jan 27, 2004
i write code
Well in Visual Studio they are macros and macros are highlighted different than identifiers or operators.
In Xcode or other standard compliant IDE's they are treated like regular operators, not macros, and are highlighted as such.

This all depends on your syntax highlighting preferences of course.

xgalaxy fucked around with this message at 18:40 on Apr 16, 2014

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Subjunctive posted:

What editor do you use that highlights the alternative tokens? They're just macros, so either it special-cases things that transitively include iso646.h and handles redefinition properly, or it's expanding macros to find those that work out to single operator tokens. Those both sound like hanging offenses.

In Python they are a core part of the language, and will be highlighted as operators. I was only pointing out that in principle, in a language, barewords can be operators and look the part.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

xgalaxy posted:

In Xcode or other standard compliant IDE's they are treated like regular operators, not macros, and are highlighted as such.

Is that the case only if iso646 is included transitively and nothing is redefined? (In the C case; they're part of the language in C++ but not C IIRC.)

Edit:

Per http://en.wikipedia.org/wiki/C_alternative_tokens#C.2B.2B

Wikipedia posted:

The abovementioned identifiers are operator keywords in the ISO C++ programming language and do not require the inclusion of a header file. For consistency, the C++98 standard provides the header <ciso646>. However the latter file has no effect, being empty. Notwithstanding some compilers, such as Microsoft Visual C++, do require the header to be included in order to use these identifiers.

Hmm.

Subjunctive fucked around with this message at 19:00 on Apr 16, 2014

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Subjunctive posted:

What editor do you use that highlights the alternative tokens? They're just macros, so either it special-cases things that transitively include iso646.h and handles redefinition properly, or it's expanding macros to find those that work out to single operator tokens. Those both sound like hanging offenses.

Or it could just have them in the syntax highlighter's hardcoded list of things to highlight and just accept that it'll sometimes highlight things it shouldn't. Syntax highlighters getting edge cases wrong is not at all unusual or a big deal.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Plorkyeran posted:

Or it could just have them in the syntax highlighter's hardcoded list of things to highlight and just accept that it'll sometimes highlight things it shouldn't. Syntax highlighters getting edge cases wrong is not at all unusual or a big deal.

Yeah, and I guess it can tell identifier from operator most of the time based on context anyway. This bothers me more than it should, I'll do some breathing exercises.

Slurps Mad Rips
Jan 25, 2009

Bwaltow!

Subjunctive posted:

Is that the case only if iso646 is included transitively and nothing is redefined? (In the C case; they're part of the language in C++ but not C IIRC.)

Edit:

Per http://en.wikipedia.org/wiki/C_alternative_tokens#C.2B.2B


Hmm.

If you pass /Za to MSVC, you can use these operators without requiring the header. However adding /Za results in compiler extensions being disabled. it's also pretty buggy and isn't being actively tested. It hasn't been deprecated either.

Gazpacho
Jun 18, 2004

by Fluffdaddy
Slippery Tilde

Suspicious Dish posted:

Yeah, I was more wondering about !=. ISO646 seems to define both ! and =.
iso646.h targets variant character sets and variant keyboards. See here for the surprising history.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!
Doesn't the addition of those things potentially break a bunch of backwards-compatibility, for any code where someone had a variable named "not" for example?

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
Yes. But if you have a variable named "not", you deserve to get your code broken.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Suspicious Dish posted:

Yes. But if you have a variable named "not", you deserve to get your code broken.
Maybe, but "or" seems pretty reasonable as a local variable name. Two letter acronym-variables with a small scope are fairly common.

Adbot
ADBOT LOVES YOU

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

roomforthetuna posted:

Maybe, but "or" seems pretty reasonable as a local variable name. Two letter acronym-variables with a small scope are fairly common.

My statement still holds.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply