Author Topic: Noob problem with std::map  (Read 38180 times)

TheCreator

  • Rogueliker
  • ***
  • Posts: 370
  • Karma: +0/-0
    • View Profile
    • Fame
    • Email
Re: Noob problem with std::map
« Reply #30 on: October 22, 2015, 08:41:18 AM »
Oh yes it will. You are likely to already know, but for the sake of other readers, I'll remind that the main problem with auto_ptr is that its assignment/copy behaviour is literally broken. It actually performs an implicit move instead of a assignment/copy, silently leaving the original empty. Assignment operator not performing assignment, this is a disaster on many levels rendering auto_ptr unusable with standard library (which relies on copying greatly) and prone to counter-intuitive results.

Maybe you are talking about using auto_ptr as a scope-level guard tasked with calling destructor. In this role auto_ptr mainly fails by not providing means to specify a custom deleter. For example, you cannot wrap FILE* or AVFormatContext* in auto_ptr and make it call suitable close function at scope exit (but you can do this with shared_ptr or unique_ptr). This leaves a single scenario where auto_ptr is useful. That would be scope-guarding a temporary polymorphic object dynamically allocated within that scope. This situation is so narrow that I believe (religious, duh!) it is much safer to just outright ban auto_ptr than keep all this quirks in mind. Raw pointers should be better than auto_ptr, if you cannot or refuse to use normal smart pointers.

Ever heard of a guy named Herb Sutter? Well, he's written an article on auto_ptr:

http://www.gotw.ca/publications/using_auto_ptr_effectively.htm

In short: auto_ptr ain't broken, it's useful, yes, it has some pitfalls, just like anything else in programming.

Quote
There are a few options. If you are not too attached to VisualStudio, there are another IDEs, i. e. Eclipse CDT (quite different but really good after you suffer through a bit). There is also Boost, which is admittedly bloated but you are not compiling by hand and paper, are you? And there is always an option to just copy-paste the relevant source from somewhere. Just saying.

I'd rather use Vim than Eclipse CDT. Eclipse may be a great IDE for Java, but it fails miserably as an IDE for C++. It would be unwise to give up something as good as Visual Studio just to get smart pointers :). As of non-standard libraries, I try to avoid them when I can. This is mainly to avoid situations when my program crashes executing some code I've never seen. It's enough for me that I sometimes need to go through STL source files. Adding another library with its different coding style, different rules, different debugging techniques, different string class etc. is just begging for more trouble.
Fame (Untitled) - my game. Everything is a roguelike.

Xecutor

  • 7DRL Reviewer
  • Rogueliker
  • *
  • Posts: 263
  • Karma: +0/-0
    • View Profile
Re: Noob problem with std::map
« Reply #31 on: October 22, 2015, 02:36:50 PM »
I'd rather use Vim than Eclipse CDT. Eclipse may be a great IDE for Java, but it fails miserably as an IDE for C++.
Wait... what???
Eclipse CDT is probably the best C++ IDE out there from indexing/code completion point of view. CLion is catching up, but it cost too much.
No other C++ IDE have indexer as good as CDT.
But, yes, it requires some configuration. All include paths must be valid, all external macro must be defined.


Cfyz

  • Rogueliker
  • ***
  • Posts: 194
  • Karma: +0/-0
    • View Profile
    • Email
Re: Noob problem with std::map
« Reply #32 on: October 22, 2015, 06:26:22 PM »
Quote from: TheCreator
Ever heard of a guy named Herb Sutter? Well, he's written an article on auto_ptr
This sounds like a faith in authoritative figure. Talk to me like an engineer, not like an adherent.

That article is old, some points are not even relevant anymore. I've actually went over all three use cases he outlines but this produced a wall of text, a lot of what I've already mentioned earlier, tl;dr auto_ptr either useless or potentially harmful.

On the point of guy named Herb Sutter (as if anyone interested in C++ wouldn't know of him), in the GotW #59 article, he demonstrates a kind of tunnel vision, applying the same approach as in the article you referenced to copy-and-swap idiom only to devastate the class interface. Compare and swap that with this much clearer solution.

Quote from: TheCreator
I'd rather use Vim than Eclipse CDT. Eclipse may be a great IDE for Java, but it fails miserably as an IDE for C++.
Somehow you got it backwards, it's Eclipse that is okay IDE for Java (IntelliJ Idea being the best), Eclipse CDT is one of the best IDEs for C++. Visual Studio without plugins is noticeable weaker in code presentation, navigation and management. I cringed in pain on the mention of Vim.
« Last Edit: October 22, 2015, 11:51:57 PM by Cfyz »

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: Noob problem with std::map
« Reply #33 on: October 22, 2015, 09:58:09 PM »
Visual Studio without plugins is noticeable weaker in code presentation, navigation and management.

Is that a fact? I never missed anything from Visual Studio. I'm using a lot of right mouse button over function etc. names and find declaration/definition/references. And 'Find in files'. I hate small details in stuff like that. Code::Blocks has too big menu in right mouse button and first two items are Run to cursor and toggle breakpoint which both are useless, only then comes same find decl.. etc. that VC has.

Cfyz

  • Rogueliker
  • ***
  • Posts: 194
  • Karma: +0/-0
    • View Profile
    • Email
Re: Noob problem with std::map
« Reply #34 on: October 23, 2015, 11:51:40 AM »
Quote from: Krice
Is that a fact?
Nothing is a fact in discussions like this.

On top of everything VS provides, it also has a lot of really helpful things. For example, on the scrollbar it marks not only positions of static things like errors/warnings and TODOs but also positions of context-dependent like search results and references to whatever the cursor is currently on (with difference in color between access and assignment, even). The whole file at a glance.

Eclipse's 'quick definition peek' is downright awesome. Upon hovering mouse over some symbol, Eclipse show the start of declaration of definition of the symbol along with directly adjacent comments (usually it is a javadoc-style documentation or field summary). If you hover over the popup a while longer it becomes scrollable and shows the entire declaration. You can take a look over the entire type interface or the method implementation with just a mouse-move. For variables it allows to see what that variable was declare-initialized with. For macros the 'peek' will show you a fully expanded version with step-by-step 'macro explore' after you hover over it a bit (a lifesaver when you working with a project with heavy macro usage). Instant code navigation.

Eclipse has templates (wrapping selected code in various predefined blocks or pasting common constructs with a few keypresses) and refactoring (renaming throughout the scope, extracting pieces, implementing stubs).

And lots of smaller things like C++ aware search, quick type hierarchy and includes browsing, 'find references' within the project only, todo markers besides TODO, easier one-button jumping between symbol declaration and definition, etc.

The one thing Visual Studio being undeniable good with is its debugger. I mostly work under Linux so I regularly forget about this stuff, but Eclipse's debugger is GDB and it doesn't perform as well under Windows.

TheCreator

  • Rogueliker
  • ***
  • Posts: 370
  • Karma: +0/-0
    • View Profile
    • Fame
    • Email
Re: Noob problem with std::map
« Reply #35 on: October 26, 2015, 08:05:35 AM »
This sounds like a faith in authoritative figure. Talk to me like an engineer, not like an adherent.

He must be crazy to use a totally broken pointer class, right? :P

Quote
That article is old, some points are not even relevant anymore. I've actually went over all three use cases he outlines but this produced a wall of text, a lot of what I've already mentioned earlier, tl;dr auto_ptr either useless or potentially harmful.

What was relevant 15 years ago, is still relevant. 15 year could broke my computer or my spine, but it could not break auto_ptr. The fact that the new shiny C++11 is out does not imply that something is wrong with C++03. (By the way, shared_ptr is also potentially harmful, I'd bet you didn't know that.)

Quote
The one thing Visual Studio being undeniable good with is its debugger. I mostly work under Linux so I regularly forget about this stuff, but Eclipse's debugger is GDB and it doesn't perform as well under Windows.

I wouldn't say it's the only thing, but I'd say it's unfortunately the crucial thing. A typical programmer spends 75% of their time debugging, so relying on tools like GDB sounds like masochism and a guaranteed way to a failure. I haven't used Eclipse since about a year, but if I remember correctly, it wasn't even able to display a std::string properly.

Fame (Untitled) - my game. Everything is a roguelike.

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: Noob problem with std::map
« Reply #36 on: October 26, 2015, 08:42:22 AM »
A typical programmer spends 75% of their time debugging, so relying on tools like GDB sounds like masochism and a guaranteed way to a failure.

I don't believe that 75% (unless the source code is C?). I'm using the debugger only in rare cases when programming more stuff at a time and then notice something doesn't work. The open/known bugs in Kaduria for example has been less than 10. Besides in my case when I'm using only SDL as external library I can setup the projects so that they can be developed at the same time with Code::Blocks (gcc) and Visual Studio.

TheCreator

  • Rogueliker
  • ***
  • Posts: 370
  • Karma: +0/-0
    • View Profile
    • Fame
    • Email
Re: Noob problem with std::map
« Reply #37 on: October 26, 2015, 10:35:25 AM »
I don't believe that 75% (unless the source code is C?). I'm using the debugger only in rare cases when programming more stuff at a time and then notice something doesn't work. The open/known bugs in Kaduria for example has been less than 10.

Is the game publicly available? Nope. So you don't know how many bugs you actually have.
Fame (Untitled) - my game. Everything is a roguelike.

Cfyz

  • Rogueliker
  • ***
  • Posts: 194
  • Karma: +0/-0
    • View Profile
    • Email
Re: Noob problem with std::map
« Reply #38 on: October 26, 2015, 10:52:43 AM »
Quote from: TheCreator
He must be crazy to use a totally broken pointer class, right? :P
Expertise in a field only gives a benefit of a doubt, not a final say.

Quote from: TheCreator
What was relevant 15 years ago, is still relevant. 15 year could broke my computer or my spine, but it could not break auto_ptr.
Not everything. Scope-guarding with auto_ptr is irrelevant. Exception safety with auto_ptr is mostly irrelevant.

Quote from: TheCreator
By the way, shared_ptr is also potentially harmful, I'd bet you didn't know that.
Enlighten me.

Quote from: TheCreator
A typical programmer spends 75% of their time debugging
YMMV? You won't be able to debug realtime (e. g. graphics, complex multithreaded logic, etc.) or situation-dependent (e. g. running on a different hardware) applications in the IDE anyways. Though in the context of the discussion, most roguelikes indeed can be debugged this way.

Ancient

  • Rogueliker
  • ***
  • Posts: 453
  • Karma: +0/-0
    • View Profile
Re: Noob problem with std::map
« Reply #39 on: October 27, 2015, 04:27:30 AM »
relying on tools like GDB sounds like masochism and a guaranteed way to a failure. I haven't used Eclipse since about a year, but if I remember correctly, it wasn't even able to display a std::string properly.
Which is even worse than gdb, because it can pretty print STL structures with an add on. It has steep learning curve but like Vim once basics are mastered I find it preferable to IDE.
Michał Bieliński, reviewer for Temple of the Roguelike

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: Noob problem with std::map
« Reply #40 on: October 27, 2015, 07:03:54 AM »
Is the game publicly available? Nope. So you don't know how many bugs you actually have.

That's why I wrote "known" bugs. And what releasing has to do with bugs? When I released Teemu no one was able to find bugs (from those 2 people who played the game). In fact I myself found the only known bug the current release version still has (the pedestal in the ancient cave). Although to be honest it must have more bugs, because when I have been programming v1.3 I've noticed how bad the current release version's code actually was (in some areas).

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: Noob problem with std::map
« Reply #41 on: November 24, 2015, 09:47:26 PM »
New version of gcc that came with Code::Blocks 15.12 has problems with map and make_pair. This line of  code:

Code: [Select]
rv=texts.insert(std::make_pair<int, Message_Text*>(id, mt));

Stops at error:

Code: [Select]
||=== Build file: Debug in Teemu (compiler: GNU GCC Compiler) ===|
C:\projects\Teemu\messpool.cpp||In constructor 'Message_Pool::Message_Pool(const char*)':|
C:\projects\Teemu\messpool.cpp|39|error: no matching function for call to 'make_pair(int&, Message_Text*&)'|
C:\projects\Teemu\messpool.cpp|39|note: candidate is:|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_pair.h|276|note: template<class _T1, class _T2> constexpr std::pair<typename std::__decay_and_strip<_Tp>::__type, typename std::__decay_and_strip<_T2>::__type> std::make_pair(_T1&&, _T2&&)|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_pair.h|276|note:   template argument deduction/substitution failed:|
C:\projects\Teemu\messpool.cpp|39|note:   cannot convert 'id' (type 'int') to type 'int&&'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Xecutor

  • 7DRL Reviewer
  • Rogueliker
  • *
  • Posts: 263
  • Karma: +0/-0
    • View Profile
Re: Noob problem with std::map
« Reply #42 on: November 25, 2015, 02:25:36 PM »
Actually instead of make_pair you need to use map::value_type:
Code: [Select]
    typedef std::map<int, Message_Text> IdToTextMap;
   IdToTextMap m;
   m.insert(IdToTextMap::value_type(id,msgptr));

or, if you compile your code using C++ standard, you can use:
Code: [Select]
  m.emplace(id,msgptr);

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: Noob problem with std::map
« Reply #43 on: November 25, 2015, 10:56:52 PM »
I also read somewhere that you simply need to remove that template part from make_pair and it seems to work.