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

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.)

Adbot
ADBOT LOVES YOU

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Sintax posted:

Im not looking to argue or anything but you misinterpreted my post and assumed I didn't know how to use a fundamental concept, then misinterpreted it again and assumed I want you to teach me how to use it. I'll stick to my programming for dummies book thank you

I mean, you wrote &warnings=std::cout. You don't know how to use a fundamental concept.

Dr Monkeysee
Oct 11, 2002

just a fox like a hundred thousand others
Nap Ghost

Xarn posted:

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

I really get the impression that stuff like this arose out of post-standardization usage. Everything looked fine on paper but then real-world cases made it obvious that some additional rules were necessary. Sorta like how SFINAE inadvertently opened the door to template metaprogramming.

Klades
Sep 8, 2011

Sintax posted:

Im not looking to argue or anything but you misinterpreted my post and assumed I didn't know how to use a fundamental concept, then misinterpreted it again and assumed I want you to teach me how to use it. I'll stick to my programming for dummies book thank you

To be fair to him, from looking at your code and trying to work out what exactly you're trying to do, I get the impression that you don't understand a lot of fundamental concepts in C++ and perhaps programming in general.
Which isn't a bad thing, everyone starts from somewhere, but right now you come across as someone who just learned how to hammer, and is eagerly trying to hammer a screw in while telling other people how precious they are for telling you you're doing something wrong.

Spatial
Nov 15, 2007

Is there a way to get Visual Studio to show only the filename when showing errors? It's really hard to read template errors when I have a project deeply nested in some weird directory.

VikingofRock
Aug 24, 2008




Is there a standard way to do RAII with temporary files, either in the STL or in boost? I could roll my own, but I'd rather use an accepted approach if possible. This is on unix and has no need for portability with windows, if that matters.

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

VikingofRock posted:

Is there a standard way to do RAII with temporary files, either in the STL or in boost? I could roll my own, but I'd rather use an accepted approach if possible. This is on unix and has no need for portability with windows, if that matters.

You mean like make a file named by a UUID generated in the constructor and then destroy that file in the destructor?

Don't really think there's a standard way to do that. Though there are a few standards on generating a UUID and making a RAII UUID-named temp file would be pretty simple.

Depending on what you want the file for, you may want some other filelike object instead, like a FIFO.

eth0.n
Jun 1, 2012

VikingofRock posted:

Is there a standard way to do RAII with temporary files, either in the STL or in boost? I could roll my own, but I'd rather use an accepted approach if possible. This is on unix and has no need for portability with windows, if that matters.

The C library tmpfile creates files that are guaranteed to get cleaned up when the FILE* it returns is closed, including on program termination (as long as abort() isn't used, which would bypass RAII anyway). This alone might meet your needs, but you could also write a simple file-closing RAII wrapper.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

On UNIX you can delete the file after opening it and it'll be cleaned up when the last fd is closed (including via abort() or a crash or sudden power off).

VikingofRock
Aug 24, 2008




I should mention that I actually do need the filename, since I need to pass it to a subprocess. If there's no standard way of doing this, maybe I'll just write an RAII wrapper around mkstemp. Thanks anyways.

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

VikingofRock posted:

I should mention that I actually do need the filename, since I need to pass it to a subprocess. If there's no standard way of doing this, maybe I'll just write an RAII wrapper around mkstemp. Thanks anyways.

Are you trying to send data to or from the subprocess? Use a FIFO.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Or it can take the fd if you're not setting CLOEXEC.

VikingofRock
Aug 24, 2008




leper khan posted:

Are you trying to send data to or from the subprocess? Use a FIFO.

So as to avoid the XY problem, I'm just gonna type out the full thing I want to do: I want to create a file, write some data to it, and then call a third-party command line utility which takes the name of the file as one of the command line arguments. That process uses the data in the file to do some analysis, and then it prints the result to stdout, which I am then reading and using in my main program. I want to make sure that the file gets deleted so that I don't have a bunch of these cluttering up the filesystem. I'm gonna be doing this O(10,000) times, across O(100) threads, and the program might get killed at any given moment because of the way that job management works on the supercomputer I'm running this on.

I think what I said before will let me accomplish this, but it's entirely possible I am missing something fundamental here, so let me know if it sounds like that's the case.

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

VikingofRock posted:

So as to avoid the XY problem, I'm just gonna type out the full thing I want to do: I want to create a file, write some data to it, and then call a third-party command line utility which takes the name of the file as one of the command line arguments. That process uses the data in the file to do some analysis, and then it prints the result to stdout, which I am then reading and using in my main program. I want to make sure that the file gets deleted so that I don't have a bunch of these cluttering up the filesystem. I'm gonna be doing this O(10,000) times, across O(100) threads, and the program might get killed at any given moment because of the way that job management works on the supercomputer I'm running this on.

I think what I said before will let me accomplish this, but it's entirely possible I am missing something fundamental here, so let me know if it sounds like that's the case.

Pretty sure that's what FIFOs are for. They're like files in the file system except they don't get written to disk so the performance characteristics should be much better. If you're doing this 10k times, it probably matters.

https://www.freebsd.org/doc/en_US.ISO8859-1/books/design-44bsd/overview-io-system.html
2.6.1

b0lt
Apr 29, 2005

VikingofRock posted:

So as to avoid the XY problem, I'm just gonna type out the full thing I want to do: I want to create a file, write some data to it, and then call a third-party command line utility which takes the name of the file as one of the command line arguments. That process uses the data in the file to do some analysis, and then it prints the result to stdout, which I am then reading and using in my main program. I want to make sure that the file gets deleted so that I don't have a bunch of these cluttering up the filesystem. I'm gonna be doing this O(10,000) times, across O(100) threads, and the program might get killed at any given moment because of the way that job management works on the supercomputer I'm running this on.

I think what I said before will let me accomplish this, but it's entirely possible I am missing something fundamental here, so let me know if it sounds like that's the case.

Does the third party command line accept input from stdin? (canonically, '-' as an input filename)
If so, you can pass data via a pipe.

VikingofRock
Aug 24, 2008




b0lt posted:

Does the third party command line accept input from stdin? (canonically, '-' as an input filename)
If so, you can pass data via a pipe.

No, unfortunately not. Actually, I was simplifying a bit in my post--what it really takes is an argument that looks like 'source_file.fits[sky=region(region_file.reg)]', where "region_file.reg" is the name of the file that I want to be temporary, and "source_file.fits" is some other file containing different data.

pseudorandom name
May 6, 2007

In that case you want mkstemp(3) or one of the related functions.

Edison was a dick
Apr 3, 2010

direct current :roboluv: only

leper khan posted:

Pretty sure that's what FIFOs are for. They're like files in the file system except they don't get written to disk so the performance characteristics should be much better. If you're doing this 10k times, it probably matters.

https://www.freebsd.org/doc/en_US.ISO8859-1/books/design-44bsd/overview-io-system.html
2.6.1

A FIFO won't work if the program does random access.

If you want Linux to guarantee that the file is not left around, then you need to unlink the file after creating it, or open it with the O_TMPFILE flag.
You can still get a file path to a file descriptor if you use /proc/self/fd/$FDNUMBER as the file path.

On the other hand, so long as you create your temporary files in /tmp and make proper use of RAII for cleanup you probably don't need to care about making the kernel ensure it's cleaned up, since on reboot those files are cleared away, and at runtime the only way for them to pile up is if the program crashes, and eventually some process will clean away that temporary file too.

ExcessBLarg!
Sep 1, 2001

VikingofRock posted:

So as to avoid the XY problem, I'm just gonna type out the full thing I want to do: I want to create a file, write some data to it, and then call a third-party command line utility which takes the name of the file as one of the command line arguments.
There's a bunch of ways to do this, with tradeoffs and varying levels of support. Which you use depends on the size of the file, the access pattern (I take the third-party program does read-only operations), and which OSes and kernel versions you have to support.

To start, Jsor asked a similar question recently in the general thread, my response:

ExcessBLarg! posted:

On Unix/Linux you can used a named pipe (FIFO). Like the "create a file" workaround it requires writing an inode somewhere to the file system with it's associated permission and cleanup issues, but you'll be writing to a pipe at least and not having to write the whole file.

On Linux (and elsewhere?) you can use an anonymous pipe (pipe(2)) and pass the path "/dev/fd/(read or write fd)" to the library. This has the advantage of not needing to write an inode, so there's no permission or cleanup issues, but it requires having /dev/fd support.

In either case you'll have to be careful about doing I/O to the pipe in sync with the library. On modern Linux the default pipe capacity is 64 kB, so if your files are always smaller than that, you usually can get away with writing the whole file to the pipe at once, then reading it at once. But you should expect that reads/writes to the pipe will block, so may need to fork or use a thread to call the library function with one side of the pipe while "simultaneously" serializing it on the other side.
That all works if the third-party program reads the file sequentially (doesn't do lseek or mmap). If the other program does do either of those two things you can't use a pipe and have to use some form of shared memory.

If you only have to support "recent" versions of Linux (3.17+), I'd look into memfd_create(2), which creates an anonymous file (not backed by any store) and returns a file descriptor that you can write/mmap, and then "pass" to the other program using /dev/fd.

If you only have to support "less recent" versions of Linux (3.11+) you can use open(2)'s O_TMPFILE argument. This does create an inode in the file system, but you don't have to worry about generating a unique name and unlinking/cleanup. If you go this route you may want to use a tmpfs filesystem (e.g., /run/shm) to avoid having the inode written to a persistent file system (disk).

If you have to support non-recent Linux or other Unixes then mkstemp(3) followed by an unlink will work, as mentioned by others. I'd still try to put this on a tmpfs.

leper khan posted:

Pretty sure that's what FIFOs are for. They're like files in the file system except they don't get written to disk so the performance characteristics should be much better. If you're doing this 10k times, it probably matters.
Data blocks don't get written to disks, but the inodes still do. If dealing with small files in large quantity (probably more than 10,000) the metadata operations around inode creation and deletion will incur runtime cost.

VikingofRock
Aug 24, 2008




As far as I know the third-party utility does need random access to the file, so I ended up writing an RAII wrapper around mkstemp/unlink. Thanks for the help guys.

ufarn
May 30, 2009
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.

raminasi
Jan 25, 2005

a last drink with no ice

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.

Well, unfortunately, Visual Studio is by far the simplest choice when doing C++ development on Windows, so your heuristic is leading you astray in this case.

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:

ufarn
May 30, 2009

Xarn posted:

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

Also use Visual Studio. :v:
ST3's build systems are just based on JSON files: http://sublimetext.info/docs/en/reference/build_systems.html

If people don't have it working on ST3, they don't have a working build system. The default build system for C++ in ST3 is specifically for single files.

Volguus
Mar 3, 2009

ufarn posted:

ST3's build systems are just based on JSON files: http://sublimetext.info/docs/en/reference/build_systems.html

If people don't have it working on ST3, they don't have a working build system. The default build system for C++ in ST3 is specifically for single files.

If you want to learn a language (as opposed to build tools) take the easiest path to learn the language: use and IDE which can help with the boring and not-so-interesting tasks of building things. Visual Studio (community edition is free) can help a lot and lets you concentrate on the language itself not on how you build things.

Yes, you should learn how to build an executable from the command line (make, cmake, msbuild or this ST3 thing that I've never heard of before) without the aid of an IDE, but that has nothing to do with the language itself. You can use make to build c, c++, java or whatever other things you want. And probably the same goes for that ST3. Build tools != language.

ufarn
May 30, 2009
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.

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

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.

IDEs make it easy to find the build button. It looks like this: |>

csammis
Aug 26, 2003

Mental Institution

ufarn posted:

all the magic involved in creating external classes in C++ is one of the things that really put me off IDEs

What? As long as you avoid MFC - and if you're learning C++ in 2016 then you probably should avoid MFC - Visual Studio does basically no magic when it comes to creating C++ code. Class creation is very straightforward and masks no part of the process.

As for your immediate problem, I think you're asking how to write one of these ST3 JSON files - which are used to invoke a compiler command - so that it will build a C++ executable whose source is contained in multiple .h/.cpp files. Is that right? If that is right and you're trying to figure out how to get your text editor to invoke your compiler, then congratulations ST3 is an IDE :haw:

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).

Joda
Apr 24, 2010

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

Fun Shoe
I remember my first run-in with MSVS for C++. I couldn't for the life of me figure out how to just run a single main file, so i gave up and kept playing around in Dev-C++. IDEs can seem inconquerable and bloated for anyone just starting up, because they're made for bigger projects, and typically left out a lot of ease of use like just being able to load in a main file and compile it without any fuss or projects or poo poo.

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!
Does Visual Studio still try to foist MFC on you if you're not consciously avoiding it, or has it become sane now?

nielsm
Jun 1, 2009



roomforthetuna posted:

Does Visual Studio still try to foist MFC on you if you're not consciously avoiding it, or has it become sane now?

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.

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.)

Plorkyeran
Mar 22, 2007

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

roomforthetuna posted:

Does Visual Studio still try to foist MFC on you if you're not consciously avoiding it, or has it become sane now?

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

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.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Well it installs the program without making your computer unusable for 8 hours in the process, so I guess so?

LLSix
Jan 20, 2010

The real power behind countless overlords

I've been writing C for the last few years and would really like to brush up on c++ for job hunting. What are some good ways/sites to practice c++ coding? I found HackerRank earlier today but so far the problems have been pretty easy and I'm worried I'll blitz through them in a week or two.

MrMoo
Sep 14, 2000

That site has hundreds of challenges and competitions every week, the quality of challenges is a bit low and the competitions are like 99% dynamic programming.

sarehu
Apr 20, 2007

(call/cc call/cc)

LLSix posted:

I've been writing C for the last few years and would really like to brush up on c++ for job hunting. What are some good ways/sites to practice c++ coding?

Make some bug fixes/performance improvements for RethinkDB.

Adbot
ADBOT LOVES YOU

Beef
Jul 26, 2004

LLSix posted:

I've been writing C for the last few years and would really like to brush up on c++ for job hunting. What are some good ways/sites to practice c++ coding? I found HackerRank earlier today but so far the problems have been pretty easy and I'm worried I'll blitz through them in a week or two.

Write a little game clone using as much modern C++ features as possible, but be sure to throw away the code afterwards and make a mental note of what worked well for you and what did not.

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