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
Zopotantor
Feb 24, 2013

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

Plorkyeran posted:

You can only partially specialize types and not functions, so to partially-specialize a function you have to make it a non-templated static member of a templated struct. You also have the syntax for partial specialization wrong; it's template<typename T> struct Bar<T, Foo> { ... }; to partially-specialize template<typename T, typename U> struct Bar;.

You could try to overload the function and use SFINAE (enable_if etc.) to select which version to use.

Adbot
ADBOT LOVES YOU

Rottbott
Jul 27, 2006
DMC

Ralith posted:

This isn't great, because you've now got a function template that will accept arguments of any type whatsoever, and then spit out a verbose template error. Instead, you can just define two non-template functions, one taking a const reference and the other taking an rvalue reference. Overload resolution will select the correct one and you can keep the implementations elsewhere if you want.
That's potentially slower though. Maybe T supports assignment from types other than T, e.g. string::operator=(const char*).

Dren
Jan 5, 2001

Pillbug
Edit: n/m

Dren fucked around with this message at 19:06 on Jun 17, 2016

Deus Rex
Mar 5, 2005

Rottbott posted:

That's potentially slower though. Maybe T supports assignment from types other than T, e.g. string::operator=(const char*).

It's potentially (but probably not meaningfully) slower, sure, but only having the templated setter would be awful for anyone using the API. APIs are for humans.

Sex Bumbo
Aug 14, 2004

Deus Rex posted:

It's potentially (but probably not meaningfully) slower, sure, but only having the templated setter would be awful for anyone using the API. APIs are for humans.

Why is having only one function signature to do a thing bad for an api? Also it would run different code so you can't really make any assertion about how much slower it will be, unless you're referring to the specific string example.

Ralith
Jan 12, 2011

I see a ship in the harbor
I can and shall obey
But if it wasn't for your misfortune
I'd be a heavenly person today

Rottbott posted:

That's potentially slower though. Maybe T supports assignment from types other than T, e.g. string::operator=(const char*).
It might be slower by a trivial amount if you assume a brain-dead compiler and/or deliberately obnoxious implementation details, sure, but it's the better solution. If nothing else you can just add the extra overloads. Obviously the class in question isn't totally unspecified here because hiding the implementation was given as an option in the first place.

Sex Bumbo posted:

Why is having only one function signature to do a thing bad for an api? Also it would run different code so you can't really make any assertion about how much slower it will be, unless you're referring to the specific string example.
A template function isn't "only one function signature," it's an infinite number of potential function signatures. That's exactly why it's way worse if at all avoidable.

Sex Bumbo
Aug 14, 2004
Okay yes, if we're going to be obtusely pedantic I mean that if you're a person using an API it seems useful when asking "how do I do X?" that you get "this is the one way to do X" if at all possible. The logic of what happens gets deferred to the types rather than the person using it.

Ralith
Jan 12, 2011

I see a ship in the harbor
I can and shall obey
But if it wasn't for your misfortune
I'd be a heavenly person today

Sex Bumbo posted:

Okay yes, if we're going to be obtusely pedantic I mean that if you're a person using an API it seems useful when asking "how do I do X?" that you get "this is the one way to do X" if at all possible. The logic of what happens gets deferred to the types rather than the person using it.
Yes, that is indeed why we have function overloading!

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

Ralith posted:

Yes, that is indeed why we have function overloading!

Are you really trying to say that templates are generally bad and should be avoided? Because that goes starkly against conventional wisdom and much of the best laid out code I've seen.

Ralith
Jan 12, 2011

I see a ship in the harbor
I can and shall obey
But if it wasn't for your misfortune
I'd be a heavenly person today

leper khan posted:

Are you really trying to say that templates are generally bad and should be avoided? Because that goes starkly against conventional wisdom and much of the best laid out code I've seen.
Introducing new templates is bad should be avoided when it's unnecessary, which is indeed most of the time. Having powerful metaprogramming available is wonderful, but it should always be your last resort, and if that's contrary to your understanding of conventional wisdom I don't know what to say.

Sex Bumbo
Aug 14, 2004

Ralith posted:

Introducing new templates is bad should be avoided when it's unnecessary, which is indeed most of the time. Having powerful metaprogramming available is wonderful, but it should always be your last resort, and if that's contrary to your understanding of conventional wisdom I don't know what to say.

Should perfect forwarding be removed from the language then? It's not truly doing anything that clients can't accomplish themselves by writing more code.

sarehu
Apr 20, 2007

(call/cc call/cc)
There is a trade-off here and the best choice depends on the situation. You trade some comfort in reading the code, making it hard to see (a) all the ways you could call the function, and (b) all the ways the function could be called, in exchange for reduced time writing the code and for the benefit of avoiding some code duplication. The error messages will be just one instantiation deeper, so that's not a real problem.

Edit: And obviously it's wrong that templates should be used as a "last resort" -- they should be used whenever they are the most economically optimal choice.

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

Ralith posted:

Introducing new templates is bad should be avoided when it's unnecessary, which is indeed most of the time. Having powerful metaprogramming available is wonderful, but it should always be your last resort, and if that's contrary to your understanding of conventional wisdom I don't know what to say.

The only things necessary for most functions are NOR and JMP, maybe some operators for reading/writing memory. That doesn't mean I shouldn't prefer the tools available to me that make things cleaner.

Generics are one of the features C++ has over C. I suppose you're also against smart pointers, custom allocators, and other things that generally make life pleasant?

sarehu
Apr 20, 2007

(call/cc call/cc)
I bet you're arguing with an interpretation of Ralith's words that doesn't reflect his/her state of mind.

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.
That could easily be the case. I'm a bit tilted because I just inherited a JavaScript project. :smithicide:

Ralith
Jan 12, 2011

I see a ship in the harbor
I can and shall obey
But if it wasn't for your misfortune
I'd be a heavenly person today

Sex Bumbo posted:

Should perfect forwarding be removed from the language then? It's not truly doing anything that clients can't accomplish themselves by writing more code.
Perfect forwarding is absolutely necessary in the cases for which it was introduced. The original problem being discussed was not one of those cases, as the type in question was statically known and presumably not doing anything super wacky with its assignment operator.

sarehu posted:

There is a trade-off here and the best choice depends on the situation. You trade some comfort in reading the code, making it hard to see (a) all the ways you could call the function, and (b) all the ways the function could be called, in exchange for reduced time writing the code and for the benefit of avoiding some code duplication. The error messages will be just one instantiation deeper, so that's not a real problem.

Edit: And obviously it's wrong that templates should be used as a "last resort" -- they should be used whenever they are the most economically optimal choice.
Poor word choice on my part--I meant "last resort" as in "only if every other possible approach is significantly less effective." Obviously if we only used the features that were strictly physically necessary to accomplish a goal we wouldn't get many goals accomplished.

leper khan posted:

The only things necessary for most functions are NOR and JMP, maybe some operators for reading/writing memory. That doesn't mean I shouldn't prefer the tools available to me that make things cleaner.

Generics are one of the features C++ has over C. I suppose you're also against smart pointers, custom allocators, and other things that generally make life pleasant?

sarehu posted:

I bet you're arguing with an interpretation of Ralith's words that doesn't reflect his/her state of mind.
:love:
RAII is a wonderful thing. Can't say I've ever had need for a custom allocator but I presume they're super convenient on occasion. The point is it's important not to use a very advanced feature when a simple one will do (almost) just as well, especially when the advanced feature has costs like worsening error messages and slowing compile time.

leper khan posted:

That could easily be the case. I'm a bit tilted because I just inherited a JavaScript project. :smithicide:
I'm so sorry.

Ralith fucked around with this message at 02:38 on Jun 18, 2016

eth0.n
Jun 1, 2012
One issue to consider with the template setter is that it's less clear what types it can accept. The signature itself doesn't say, and the set of acceptable types depends on the underlying type of the data member (or other details of the implementation if not a simple assignment). A change in the implementation can easily cause subtle differences in what usages of the setter are well-formed. With the overloaded setter, the types are fixed, and can be easily totally different from the underlying implementation. It provides stronger encapsulation.

It's not a showstopping issue, but overall, for a purpose like this, overloaded is very likely the better option, in spite of the aesthetic ugliness of having two almost identical functions.

b0lt
Apr 29, 2005

eth0.n posted:

One issue to consider with the template setter is that it's less clear what types it can accept. The signature itself doesn't say, and the set of acceptable types depends on the underlying type of the data member (or other details of the implementation if not a simple assignment). A change in the implementation can easily cause subtle differences in what usages of the setter are well-formed. With the overloaded setter, the types are fixed, and can be easily totally different from the underlying implementation. It provides stronger encapsulation.

It's not a showstopping issue, but overall, for a purpose like this, overloaded is very likely the better option, in spite of the aesthetic ugliness of having two almost identical functions.

Just use concepts, they'll be there in C++0x 11 14 17 some version in the near future!

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe
I want to start developing commercial software, which means my educational license for CLion isn't really gonna cut it anymore. Before spending €99 a year on a commercial license, I thought I'd ask if there are any good, free alternatives that work on Windows, Linux and OSX? Preferably with CMake as the primary system for project management, and it has to work with MinGW, gcc and its standard library >=4.8.

Joda fucked around with this message at 21:17 on Jun 21, 2016

fritz
Jul 26, 2003

Joda posted:

I want to start developing commercial software, which means my educational license for CLion isn't really gonna cut it anymore. Before spending €99 a year on a commercial license, I thought I'd ask if there are any good, free alternatives that work on Windows, Linux and OSX? Preferably with CMake as the primary system for project management, and it has to work with MinGW, gcc and its standard library >=4.8.

Just shell out for the commercial license.

Volguus
Mar 3, 2009

Joda posted:

I want to start developing commercial software, which means my educational license for CLion isn't really gonna cut it anymore. Before spending €99 a year on a commercial license, I thought I'd ask if there are any good, free alternatives that work on Windows, Linux and OSX? Preferably with CMake as the primary system for project management, and it has to work with MinGW, gcc and its standard library >=4.8.

QtCreator. Light years ahead of CLion. I personally prefer KDevelop on linux though (not sure if KDevelop works on other platforms).

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe

Volguus posted:

QtCreator. Light years ahead of CLion. I personally prefer KDevelop on linux though (not sure if KDevelop works on other platforms).

Worked with it when I took Realtime Graphics, and I honestly like CLion better. That said, if it was free, I'd definitely go for it, but as far as I've been able to tell you have to pay for a commercial license with QtCreator as well.

xgalaxy
Jan 27, 2004
i write code

Volguus posted:

QtCreator. Light years ahead of CLion. I personally prefer KDevelop on linux though (not sure if KDevelop works on other platforms).

QtCreator is poo poo. I hate that loving thing.
I have an emotional response whenever QtCreator is mentioned. Sorry.

Volguus
Mar 3, 2009

Joda posted:

Worked with it when I took Realtime Graphics, and I honestly like CLion better. That said, if it was free, I'd definitely go for it, but as far as I've been able to tell you have to pay for a commercial license with QtCreator as well.

As far as I can tell it is very much free to use for whatever projects you want. However, since I'm not a lawyer ... take this with some salt.

xgalaxy posted:

QtCreator is poo poo. I hate that loving thing.
I have an emotional response whenever QtCreator is mentioned. Sorry.

That's fine. I have the same response whenever a JetBrains tool gets mentioned, more so when the advice is: pay for it. The only response to a JetBrains tool is "I can't believe they're actually charging money for it" and "I can't believe people are willing to part with their money for that".

Use VI, emacs, Eclipse, Netbeans, nano, notepad, anything. Really, anything but JetBrains.

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe

Volguus posted:

As far as I can tell it is very much free to use for whatever projects you want. However, since I'm not a lawyer ... take this with some salt.

On their website they're asking like $80 a month for a commercial license. In general their licensing seems to be some weird, incomprehensible mess of LGPL and GPL, The way I'm interpreting this is that if I want to distribute a commercial, closed-source application I need to have the commercial license.

Also, w/r/t JetBrains, I only ever worked with CLion and it honestly works great (at least for my purposes,) has good GDB support and integration, and (unlike QtCreator,) has an easily installable, up-to-date GLSL plugin.

Volguus
Mar 3, 2009

Joda posted:

On their website they're asking like $80 a month for a commercial license. In general their licensing seems to be some weird, incomprehensible mess of LGPL and GPL, The way I'm interpreting this is that if I want to distribute a commercial, closed-source application I need to have the commercial license.

The way I am interpreting it is that you can re-redistribute your application as long as your are conforming to the GPL/LGPL licenses. For a commercial application you really don't want to touch GPL libraries. However, it is perfectly fine to use an LGPL library as long as your are dynamically linking to it. No static linking.
The additional restriction is : any changes you make to an LGPL library you must publish the source code for that if you are redistributing your application (and that library).
If you buy the license you are essentially buying support and the ability to change Qt and not publish the changes (and that is a perfectly reasonable and valid way to go for many companies).

Other than that, you are free as a bird.

Klades
Sep 8, 2011

Volguus posted:

As far as I can tell it is very much free to use for whatever projects you want. However, since I'm not a lawyer ... take this with some salt.


That's fine. I have the same response whenever a JetBrains tool gets mentioned, more so when the advice is: pay for it. The only response to a JetBrains tool is "I can't believe they're actually charging money for it" and "I can't believe people are willing to part with their money for that".

Use VI, emacs, Eclipse, Netbeans, nano, notepad, anything. Really, anything but JetBrains.


xgalaxy posted:

QtCreator is poo poo. I hate that loving thing.
I have an emotional response whenever QtCreator is mentioned. Sorry.

Out of curiosity, for what reasons do you hold such vitriol toward these software packages? I've never used either, other than JetBrain's resharper plugin which makes working with C++ in visual studio quite a bit better. Haven't paid for the thing, though.

xgalaxy
Jan 27, 2004
i write code

Klades posted:

Out of curiosity, for what reasons do you hold such vitriol toward these software packages? I've never used either, other than JetBrain's resharper plugin which makes working with C++ in visual studio quite a bit better. Haven't paid for the thing, though.

I had to use QtCreator once for a job and it was an extremely frustrating experience. Bugs, crashes, debugger showing incorrect data, etc, etc. It was to the point where it would make my blood boil and now whenever it is mentioned all of those emotions come rushing back.

Eclipse is a better C/C++ IDE than QtCreator. I have no problems with CLion and I like that all of JetBrains products provide a consistent frontend across all of their products which is great for a someone like me who works in multiple languages. Also every JetBrains products have a pretty good VIM emulation plugin.

Volguus
Mar 3, 2009

Klades posted:

Out of curiosity, for what reasons do you hold such vitriol toward these software packages? I've never used either, other than JetBrain's resharper plugin which makes working with C++ in visual studio quite a bit better. Haven't paid for the thing, though.

Just like xgalaxy before me I had (and have to) use JetBrains products for the job. Thus, exposed to all its bugs, missing features, crashes and so on.
In all honesty, if you're looking closely, all products have problems. Eclipse, Visual Studio, Netbeans and even QtCreator. However, I suppose is just a matter of what bugs can you live with vs what bugs annoy the living poo poo out of you. When one is switching IDEs is just switching one set of problems for another. So, when the recommendation comes: "go pay for X, is better", well hold on!. It may be better for you, as the problems of X do not affect you or at least you can live with them, it may not be better for others. Then there is that other psychological factor that is creating that recommendation: "I paid $ for this, therefore it must be good". And is a self-reinforcement, self-sustaining idea, which in a lot of cases sweeps under the rug valid and big problems with the paid software.

The first recommendation should always be: go try the free alternatives. If they don't give you what you want, try the trial version of the paid product. If it is something that you think you need, then by all means, go and buy it. To fork money for a product should always be the last resort option, when all other alternatives have been explored. Otherwise the one making the recommendation sounds like a lovely car salesman.

netcat
Apr 29, 2008
Just use eclipse.

Rottbott
Jul 27, 2006
DMC

Deus Rex posted:

It's potentially (but probably not meaningfully) slower, sure, but only having the templated setter would be awful for anyone using the API. APIs are for humans.
Indeed, I wouldn't use the template version myself. But the original question did ask for the absolute fastest method.

seiken
Feb 7, 2005

hah ha ha
If your requirement is CMake and cross platform, you could just use Visual Studio (free) on Windows and your choice of vim/emacs on the others. CMake generates Visual Studio project files; the other platforms just work from the command line. This gives you the most canonical sort of development environment on each platform and you don't have to use any of the crap IDEs everyone is arguing about.

If you use vim get YouCompleteMe and then it basically has all the useful bits of an IDE too

feedmegin
Jul 30, 2008

Volguus posted:

The way I am interpreting it is that you can re-redistribute your application as long as your are conforming to the GPL/LGPL licenses. For a commercial application you really don't want to touch GPL libraries. However, it is perfectly fine to use an LGPL library as long as your are dynamically linking to it. No static linking.
The additional restriction is : any changes you make to an LGPL library you must publish the source code for that if you are redistributing your application (and that library).
If you buy the license you are essentially buying support and the ability to change Qt and not publish the changes (and that is a perfectly reasonable and valid way to go for many companies).

Other than that, you are free as a bird.

Yes, this is correct. It's been a long, long time since you needed to pay Trolltech/Nokia/the Qt company to use Qt for commercial/closed-source development. Nor do you have to pay them just to use Qt Creator, :wtc:

Xarn
Jun 26, 2015

Joda posted:

I want to start developing commercial software, which means my educational license for CLion isn't really gonna cut it anymore. Before spending €99 a year on a commercial license, I thought I'd ask if there are any good, free alternatives that work on Windows, Linux and OSX? Preferably with CMake as the primary system for project management, and it has to work with MinGW, gcc and its standard library >=4.8.

Cmake to generate makefiles/project files and VS for development. :v:

I am actually mostly serious, VS 2015 is p. nice, free for commercial use and you can always switch by having CMake generate different poo poo.

Klades
Sep 8, 2011

seiken posted:

If you use vim get YouCompleteMe and then it basically has all the useful bits of an IDE too

YCM (and anything else relying on clang) will absolutely poo poo the bed if your codebase does anything weird enough, but hopefully if you're starting a new project with modern C++ it shouldn't come up.

(I've entertained the thought that it's a configuration problem or fixable bug, but when I opened an issue on the matter the YCM guys just went "clang problem not ours good day")

Edison was a dick
Apr 3, 2010

direct current :roboluv: only

feedmegin posted:

Yes, this is correct. It's been a long, long time since you needed to pay Trolltech/Nokia/the Qt company to use Qt for commercial/closed-source development. Nor do you have to pay them just to use Qt Creator, :wtc:

Except that now they are making new libraries L?GPLv3, which makes it unusable for some embedded projects.

Xarn
Jun 26, 2015

Klades posted:

YCM (and anything else relying on clang) will absolutely poo poo the bed if your codebase does anything weird enough, but hopefully if you're starting a new project with modern C++ it shouldn't come up.

(I've entertained the thought that it's a configuration problem or fixable bug, but when I opened an issue on the matter the YCM guys just went "clang problem not ours good day")

Does Clang also poo poo the bed during compilation? If yes, Clang problem, if not, I'd give it about 3/5 of being configuration problem versus being problem in the libclang.


For a little bit of content, does anyone here work with clang-tidy regularly?

I have a toy .cpp file:
code:
#include <algorithm>
#include <vector>
#include <iostream>

int main(){
    std::vector<int> vec{1, 2, 3, 4, 5, 6};
    vec.erase(std::remove_if(begin(vec), end(vec),
                             [](int x){ return x % 2 == 0;}));

    std::cout << vec.size() << '\n';
}
which, according to my reading of the documentation should trigger misc-inaccurate-erase check, but when I run
code:
clang-tidy -checks="misc-inaccurate-erase" bad-erase.cpp -- -std=c++14
it reports nothing at all. If I tell it to run all possible checks, it complains that the includes aren't sorted properly :v:. I am running a clean Ubuntu 16.04 VM.

b0lt
Apr 29, 2005

Xarn posted:

Does Clang also poo poo the bed during compilation? If yes, Clang problem, if not, I'd give it about 3/5 of being configuration problem versus being problem in the libclang.


For a little bit of content, does anyone here work with clang-tidy regularly?

I have a toy .cpp file:
code:
#include <algorithm>
#include <vector>
#include <iostream>

int main(){
    std::vector<int> vec{1, 2, 3, 4, 5, 6};
    vec.erase(std::remove_if(begin(vec), end(vec),
                             [](int x){ return x % 2 == 0;}));

    std::cout << vec.size() << '\n';
}
which, according to my reading of the documentation should trigger misc-inaccurate-erase check, but when I run
code:
clang-tidy -checks="misc-inaccurate-erase" bad-erase.cpp -- -std=c++14
it reports nothing at all. If I tell it to run all possible checks, it complains that the includes aren't sorted properly :v:. I am running a clean Ubuntu 16.04 VM.

Are you using a sufficiently new version?

VikingofRock
Aug 24, 2008




Xarn posted:

Does Clang also poo poo the bed during compilation? If yes, Clang problem, if not, I'd give it about 3/5 of being configuration problem versus being problem in the libclang.


For a little bit of content, does anyone here work with clang-tidy regularly?

I have a toy .cpp file:
code:
#include <algorithm>
#include <vector>
#include <iostream>

int main(){
    std::vector<int> vec{1, 2, 3, 4, 5, 6};
    vec.erase(std::remove_if(begin(vec), end(vec),
                             [](int x){ return x % 2 == 0;}));

    std::cout << vec.size() << '\n';
}
which, according to my reading of the documentation should trigger misc-inaccurate-erase check, but when I run
code:
clang-tidy -checks="misc-inaccurate-erase" bad-erase.cpp -- -std=c++14
it reports nothing at all. If I tell it to run all possible checks, it complains that the includes aren't sorted properly :v:. I am running a clean Ubuntu 16.04 VM.

Wait, what's wrong with that code? I checked the misc-inaccurate-erase documentation but I think I must still be missing something.

Adbot
ADBOT LOVES YOU

Klades
Sep 8, 2011

Xarn posted:

Does Clang also poo poo the bed during compilation? If yes, Clang problem, if not, I'd give it about 3/5 of being configuration problem versus being problem in the libclang.

Couldn't say; the actual compliation is done with gcc. I wouldn't be at all shocked if it was a configuration issue, and would in fact be quite pleased since that would mean it could be fixed.
The biggest problems I have with it are when the implementation of template classes (or inline methods) are put in separate files which are included by the header (is this even a good way to do it? No idea, but that's what the codebase I have does), dealing with preprocessor voodoo (hello ACE), and getting it to find headers properly. I found a rather clunky workaround for the last thing (found a ycm_extra_config designed to be more-or-less project agnostic, then made an include directory with a bunch of symbolic links to the actual header paths, ugh), though vim itself still can't find the files (so no gf-ing into them from other code).
I don't know what to do about the others though. ACE sets up a bunch of macros all over the place, including ones for import/export flags for dynamic libraries. Linux doesn't need them, but Windows does, so any class that's going to be exported looks like class FOO_Export Foo : Foo_Base, and as far as I can tell the clang analyzer does not care for this much and it makes my syntax coloring plugin (which also uses clang) barf yellow everywhere if the class is inside a namespace.

The main thing that bugs me about it all is that VS will happily just deal with all of that. Preprocessor feature guards? It will process them, regardless of how deeply nested they are and how many files it has to chase down, so you can mouse hover and see what they'll actually evaluate to, and it'll even show you what blocks of code are made inactive by an #if. Template implementation separate from declaration? It doesn't really seem to care. And getting it to find the scattered header files is just a matter of setting some environment variables and launching it with /useenv.

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