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
Xarn
Jun 26, 2015
If I understood you correctly, you should just const_cast away the constness internally to support both const and nonconst interface.

This is one of the few cases where it is completely sane and expected to use const_cast.

Adbot
ADBOT LOVES YOU

Xarn
Jun 26, 2015
I am not entirely sure this falls under scope of this thread, but this seems like the best place to ask anyway.

I have a chance to significantly alter the C/C++ course taught at my university. Its current state is the usual C89+C++98 basics that needlessly repeat things from prerequisite courses and then end before saying anything useful about templates. Obviously I would prefer to burn things to the ground and start anew, but that is not an option (yet :ssh:) so I am going to try and get C dropped, because there isn't enough time to do modern C AND C++ (hell, there isn't enough time to do C++ properly, but that is another story and another fight) and everything currently in the course is covered by direct prerequisite. I am also going to push to update C++ part to C++11 with C++14 elements (basically taking the current MSVC as the least common denominator).

So now I am looking for ideas (You HAVE to teach r-value references), opinions (You are dumb for dropping C) and examples of high quality C++ courses (Stroustrup's PPP using C++ seems pretty good) to use as references for the changes.

Xarn
Jun 26, 2015

nielsm posted:

Is that course a general "introduction to programming" course, or is it intended for students who already know other languages to learn C and/or C++?

No, the "introduction to programming" is the (two semester) prerequisite. However the current state feels a lot like it is.

Xarn
Jun 26, 2015

Blotto Skorzany posted:

What is the title of that C-and-C++ course, and what is its ultimate purpose?

The title is "Programming in C/C++" and the purpose is to teach someone who supposedly has at least basic understanding of programming in some other language some C++. Ideally it would give large degree of competency, but without getting more time, the target is usable competency. The closest to proper requirement on the course is that the computer vision guys would really like course that teaches their students to take Matlab prototype and make something roughly demoable.

Its not a formal prerequisite for any other course, and it isn't informal prerequisite for any courses either. Some courses want their student to understand C, but there is already a course to help catch up with C in low level environment. The only course to take an informal dependency in the near future is going to be advanced C++ and that is only if someone pushes is through. (It is currently blocked by academic elitism and politics)

Xarn
Jun 26, 2015

Plorkyeran posted:

There's a few people pushing to take advantage of the backwards-compatiblity break when the standard library is Conceptified to clean up all the old cruft and bad decisions, but I suspect that'll get bogged down in endless committees.

My money is on minor cleanups being done and then a fuckton of bikeshedding blocking anything interesting.

Xarn
Jun 26, 2015
Can someone explain that to someone lucky enough to get started with git as source control?



Ekster: I think you would learn more by thinking up data structure implementation from high/mid level description, rather than from reverse engineering some horrific C library.

Xarn
Jun 26, 2015

hackbunny posted:

Because tag-based dispatch is awful
Word.


Anyway, with the concepts coming, STL might be able to take a compatibility break and fix some things being wrong. Core language is not so lucky, it did take until C++17 to get rid of freaking trigraphs. So lets hope for the best? :suicide:

Xarn
Jun 26, 2015
Two CcpCon 2015 videos are already up at their youtube channel.

They are about Stroustrup's and Sutter's modern coding style guidelines, and sound pretty interesting. (Sutter's much more so)

Xarn
Jun 26, 2015

xgalaxy posted:

I don't quite follow Sutter's argument that Rust requires annotations just after showing C++ code that requires annotations. Anyway, pretty neat to see this stuff actually working and it sounds like it will be in an update in Visual Studio before the end of the year.

I think his argument was that annotations should be exception, not norm.

Xarn
Jun 26, 2015
Well, it doesn't seem to be worth its own thread

Xarn
Jun 26, 2015
First of all, if you have access to C++11 I heartily recommend using std::function instead.

Anyway, my guess would be that since your Task<T> has naive copy constructor and assignment, you tried returning it and it outlived the original object.

Xarn
Jun 26, 2015
What is a nice and sane C++ concurrency library? This means higher level than <thread>, <mutex> et al. and less macroy than OpenMP pragmas.

Bonus points if it runs on Windows as well as on Linux.

Xarn
Jun 26, 2015

Subjunctive posted:

Maybe check out Intel TBB? It's not to everyone's tastes, but I recall being generally happy with it.

TBB is what I definitely plan on checking out, but I was looking for alternatives.

Ralith posted:

What exactly do you want it to do for you?

Give a reasonably simple task oriented interface... IE being able to chain tasks, not having to explicitly manage threads for simple async stuff.

Xarn
Jun 26, 2015
Is there any (preferably sane) way to have Windows and Linux computers work together using OpenMPI? I am using MSMPI on Windows.

Xarn
Jun 26, 2015
code:
world_to_object = Matrix4f::inverse(object_to_world)
Seems clear enough.

Xarn
Jun 26, 2015

Illusive gently caress Man posted:

No, it's pretty simple to write an algorithm linear in the sizes of the sets (use iterators), but it sounds like the kind of thing the standard library would have, and possibly be better than what I write.

edit: I'm assuming set iterators are at least constant amortized time to increment. That is true, isn't it?

edit2: I found a way to make one of them an unordered set so w/e problem solved

Yeah, linear time is easy and it is up to you to implement it. I would also expect it to be faster than having one of the set be unordered and do lookups in it.

Xarn
Jun 26, 2015
What do I need to find out and replicate in a VM, to be able to precompile .o files in a VM that will link properly when used on the real machine?

I already know that it is running Debian Jessie, is using g++ 4.9.2 and should be able to find out anything else I will need to.

Xarn
Jun 26, 2015

pseudorandom name posted:

Why aren't you doing the final link in the VM?

Also, ABI-compatible isn't sufficient for glibc, the version of glibc that the relocatable objects were built against needs to be identical to the version that they're finally linked against.

Because the whole thing is crazy ...


Basically, my university has a system for uploading of student's semestral work/homeworks, etc. Bolted on top of it is a system for automatic grading -- i.e. for programming classes you upload your implementation of $homework and it then gives you a grade (points for the final grade). So, this semester I decided to go and help modernize and teach our C++ course and one of the things we decided to do is to have a lot of smallish homeworks that are automatically graded.

To be nice to the students, we are using Catch for behavior tests (and we also do some more behind-the-scenes validation for correctness, i.e. use Valgrind) and give them the tests, so they can have a quicker feedback cycle than uploading it through the web interface, waiting for results and so on. However, Catch's main takes quite a long time to compile (g++ on my notebook takes 8.5s, on the server it needs 10+) and to keep the time it takes to validate student's submission short, we want to precompile it (and precompile everything else we can, while we are at it).

The fun part is, I am not allowed SSH access to the grading server (because of technical debt and lots of bureaucracy, long story), but we already got this much working -- we just had to bother someone who is allowed access to copy over a file and compile it into .o file once a week. This worked until last week, when we asked for a C++14 compiler and were told to set the grading system to use different server (the automatic evaluation system has a master server, which takes care of accepting submissions, copying it to a shared folder and farming out the actual work to different slave servers), which has different software configuration.

As we weren't able to get someone with access to this server to help us with precompiling stuff, we decided to create a VM that would allows us to create compatible .o files and then copy them to the server we have access to, because the course's folder is automagically shared with the server doing the actual evaluating.

---edit---

And no, I cannot change the evaluation script to copy back the .o files, because the moment evaluation starts, the shared folder is read-only.

Xarn fucked around with this message at 20:14 on Mar 26, 2016

Xarn
Jun 26, 2015

Volguus posted:

No, that's not crazy, that's loving insane.

And thats even without the dumb bugs I found in the ~4 weeks I am working with the system. :v:

Well at least next semester the big "rewrite" will be done. The rewrite will bolt on access control (using faculty hosted gitlab) for uploading evaluation scripts, the lovely backend will stay. :suicide:

Xarn
Jun 26, 2015
My fight with the system for automatic evaluation continues. I have given up on creating a link-compatible VM, because the target has moved and some changes in the way our own script works makes it impractical right now (if there was an easy way to get the precompiled .o files, we would would make them work, but right now :effort:). But I have new question:

Is there a way to get Valgrind working with compressed debug data? Currently it spits out a whole ton of
code:
--4273-- WARNING: Serious error when reading debug info
--4273-- When reading debug info from /lib/x86_64-linux-gnu/libc-2.22.so:
--4273-- Ignoring non-Dwarf2/3/4 block in .debug_info
which is annoying. Luckily, as far as I can see this only hits libc, libm and ld, which should mean that the user's code should still have nice stack traces, but I would prefer to solve the problem properly (ie not by filtering out these lines).

Xarn
Jun 26, 2015

Cory Parsnipson posted:

Does anyone have tips/best practices/reading for converting a project of non-trivial size from using raw pointers to using smart pointers? I want to see if I can introduce smart pointers module by module or something into an existing project that only uses raw pointers, but that seems like I could shoot myself in the foot that way.

When we tried it, we started using smart pointer everywhere in new code and slowly changed the old code as we did refactoring. It mostly worked, although there was some initial pain as noone actually understood the old ownership model, so suddenly there were double free etc.

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.

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.

Xarn
Jun 26, 2015

b0lt posted:

Are you using a sufficiently new version?

code:
~$ clang-tidy --version
LLVM ([url]http://llvm.org/[/url]):
  LLVM version 3.8.0
  
  Optimized build.
  Built Apr 21 2016 (14:10:42).
  Default target: x86_64-pc-linux-gnu
  Host CPU: broadwell
I would hope so.

Xarn
Jun 26, 2015
C++17 is shaping up to be interesting even with modules and concepts removed.

code:
if (auto [a, b] = foo(); a.valid()) {
    // do stuff with a, b
} else {
    // do different stuff with a, b
}
:2bong:

Xarn
Jun 26, 2015

Cory Parsnipson posted:

I want to make a Node class to build trees and have a template parameter "HasParentPointers" that will let me enable parent pointers or not in the Node class. Is it possible, when HasParentPointers is false, to have the Node class not even contain a pointer member? Basically, I want to use templates to make a node class that either has a parent * member or not.

Assuming that you want to have a tree of either nodes with parent pointers, or nodes without parent pointers, not mix and match, it is definitely possible, but kinda painful in C++14 (and pre C++14 as well). You will end up writing the code twice, using template specialization.

It should be p. easy in C++17, but thats not quite out or production ready yet.

Xarn
Jun 26, 2015

leper khan posted:

I think you could do it with the policy based design template template parameters outlined in Alexandrescu's Modern C++. Is that what you're referring to, and if so what's coming in 17 (I haven't been keeping up)?

I would just write the two specializations, as I see policies as useful for user customization without combinatoric explosion if you have more parameters, but there isn't much user customization to be done here.

code:
template <bool hasParentPointer>
struct node;

template <>
struct node<true> {
    node* parent = nullptr;
    node* left_child = nullptr;
    node* right_child = nullptr;
    int data;
};

template <>
struct node<false> {
    node* left_child = nullptr;
    node* right_child = nullptr;
    int data;
};

class large_tree {
public:
    size_t size() const {
        return sz;
    }
private:
    node<true>* root = nullptr;
    size_t sz;
};

class small_tree {
public:
    size_t size() const {
        return sz;
    }
private:
    node<false>* root = nullptr;
    size_t sz;
};

int main() {
    large_tree lt;
    small_tree st;
}

You will end up with some boilerplate and a whole lot of writing very similar code.

C++17 adds
code:
if constexpr {...}
As far as I know, you cannot use it to modify which class members are present in a class, but it should still save you a lot of repeated code and boilerplate, when used like this:

code:
class parametric_tree<bool hasParentPointer> {
public:
    using node_type = node<hasParentPointer>;

    size_t size() const {
        return sz; // always the same
    }
    
    void print() const {
        if constexpr(hasParentPointer) {
            // use parent pointer while traversing the tree
        } else {
            // cant use parent pointer while traversing the tree
        }
    }
    
    void insert(int elem) {
        // find place to insert the element...
        
        auto* new_node = new node_type{};
        new_node.data = elem;
        insert_at = new_node;
        if constexpr(hasParentPointer) {
            new_node.parent = ...;
        }
    }


private:
    node_type* root = nullptr;    
    size_t sz;
};

There are some pre C++17 ways of deduplicating the code, but it always felt like causing more problems, than solving. Also note that I don't have access to C++17 compiler, so take the above with a grain of salt.

Xarn
Jun 26, 2015
IIRC that should be fixed in C++17 :v:

Xarn
Jun 26, 2015

Harik posted:

Someone doublecheck me on this one:

C++ code:
#include <vector>
#include <cstdio>

int main() {
	std::vector<std::vector<int>> test;

	test.push_back(std::vector<int>());
	auto & ref = test.back();
	ref.emplace_back(1);
	ref.emplace_back(2);
	ref.emplace_back(3);
	test.push_back(std::vector<int>());
	ref = test.back(); /* Use after free here? */
	ref.emplace_back(4);
	ref.emplace_back(5);
	ref.emplace_back(6);

	for (auto & v1 : test)
		for (auto & v2 : v1)
			printf("%d\n", v2);

	return 0;
}
If I understand right, I'm blowing things up because I'm reusing a reference variable which ends up blowing away the vector in test[0]? I don't understand why that's a use-after-free, though, it should end up being the same as test[0] = test[1] - destroying the contents, but not the vector itself.

The code itself was a brainfart that I fixed already, I meant to use a pointer and used a reference instead.

When adding elements to a vector, it will have to reallocate the underlying storage sooner or later. What will happen with the memory it used to use at that point?

Xarn
Jun 26, 2015

Harik posted:


Thanks everyone for reminding me about reallocate. Of course that would cause a crash and not just changing the wrong vector.
If you are lucky that is, this isn't Java and we don't have safe memory accesses :v:

Xarn
Jun 26, 2015

Joda posted:

Is it a new thing in g++ 6.0 that it won't accept std::string() as an argument to a function that expects std::string&?

No, a non-const ref shouldn't bind to a temporary since forever, because that is almost certainly an error.

Xarn
Jun 26, 2015

Spatial posted:

Also while I'm complaining about MSVC, lmao that their implementation of deque<T> allocates buckets smaller than a cache line and degrades to a linked list if T is bigger than 64 bits.

IIRC, only libc++ has sane deque<T>, libstdc++ also has a predetermined bucket size and then just divides it to elements. There is some scary poo poo in old C++ stdlib implementations.

Xarn
Jun 26, 2015

Plorkyeran posted:

Not being able to bind an lvalue reference to a temporary was a restriction added fairly late in the standardization process, and it's a rare example of C++ forbidding something because it's a footgun rather than because it can't work.

Maybe even the only one? :v: (I still think that the rule-of-3 should've been language level rule.)

Xarn
Jun 26, 2015

ufarn posted:

I can't get C++ to work in ST3 on Win10 x64 with external class files (Class.h and Class.cpp) and my current build system. Is there a build-system solution to this or something else?

I'm not a big fan of handing over everything to an IDE when I'm picking up a new language.

Without any information about your build system, what kind of help do you expect?

Also use Visual Studio. :v:

Xarn
Jun 26, 2015

ufarn posted:

I actually think IDEs make things a lot worse when it comes to picking up a new language. Maybe if it's the first time you try coding and want to move a turtle in different directions, but all the magic involved in creating external classes in C++ is one of the things that really put me off IDEs. I don't have a problem with using them for programming in general, I just don't see the point of programs where you struggle to even find the build button.

Put up or shut up.

Either go with "blah blah, IDEs are too magic" and don't try to use ST3's weird, bespoke JSON build tool, invoking cl.exe from cmd instead (its not actually that hard), or admit that you just want ST3, there is nothing wrong with that (well, except the build tool seems poo poo).

Xarn
Jun 26, 2015

nielsm posted:

You only get MFC if you use the "MFC Application" wizard, or select the MFC box in the "Win32 Project" wizard settings for a DLL or static library type project.

It does force precompiled headers on you, unless you choose "Empty project" in the "Win32 Project" wizard. Which means you will be confused about what stfafx.h and stdafx.cpp are doing in your project, and why those seemingly pointless files are required to build.

It only forces precompiled headers if you select Win32 project -> Windows application subtype, otherwise it just has it checked by default. (No idea why it still thinks that stdafx.h is neccessary for Windows application, and really should let you just uncheck it.)

Xarn
Jun 26, 2015

Plorkyeran posted:

The upcoming version doesn't even install MFC by default anymore.

Is the new installer any good? I just remembered I got an email while on vacation to test it.

Xarn
Jun 26, 2015

Shy posted:

I don't know if that's better suited for the general programming thread but I've been meaning to ask about C++ standard/compiler progress over the last 15-20 years. If I understand it correctly C++11 and newer is mostly supported across all modern compilers and performs well, but let's say 15 years ago would a team choosing it over C for a large project encounter many additional difficulties compared to now?
I'm not looking for a detailed answer (though if you have an article handy that would be cool), mostly just want to get an idea of how bad C++ compilers used to be compared to C. My C++ experience is reading the first 200 pages of the stroustrup book :downs:

Depends a lot on how much back do you want to go.

It is also important to realize that compilers and code tends to push each other... as compilers get better at aggressively inlining code created by nested templates, people get better at creating nested templates for crazy compile time magic. :v:

Xarn
Jun 26, 2015

GeneralZod posted:

code:
assert(!sequence.empty());
Intent, people! (plus efficiency, if someone invisibly changes the vector to a pre-C++11 std::list :v:)

On one hand, I still think that part of the spec was actually genius, because of what it meant for splicing.

On the other, I've seen way too many people try to do
code:
if (list.size() > 0)
instead of
code:
if (!list.empty())

Adbot
ADBOT LOVES YOU

Xarn
Jun 26, 2015
If you really want to go full performance mode, align your data highly and vectorize that poo poo. :2bong:

(Actual results not guaranteed, some poo poo might not be vectorizable or requires major rethinking of your data layout)

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