0
std::map<double, std::pair<int, int>> templates;

this is the variable I declared to hold temporary values while performing some operation.

if I declared this as

struct TemplateIndex
{
  int i,
  int j
}; 

std::map<double, TemplateIndex> templates;

would this increase compile time performance?, any advantages or disadvantages?

S.Frank Richarrd
  • 488
  • 1
  • 3
  • 15
  • 2
    Does this answer your question? [What is the difference between using a struct with two fields and a pair?](https://stackoverflow.com/questions/2236182/what-is-the-difference-between-using-a-struct-with-two-fields-and-a-pair) – Ch3steR Feb 26 '21 at 07:31
  • Relateed: https://stackoverflow.com/a/3607745/12416453 – Ch3steR Feb 26 '21 at 07:32
  • @Ch3steR no they haven't discussed about compile time performance – S.Frank Richarrd Feb 26 '21 at 07:38
  • 1
    If it means you can `#include` less standard headers in your code, it could probably speed up compilation time. My guess is that the difference would be very small though. If compilation time is that important, you probably have a fairly big code-base that you could try on and see. – super Feb 26 '21 at 07:57
  • Note: using `double`/`float` as key is "dangerous" (rounding, ...). – Jarod42 Feb 26 '21 at 09:17

3 Answers3

0

In GNU libstdc++ std::pair is a template-based struct with two elements. Look here:

https://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.3/a02030.html

So I don't think there is much a difference.,

Hubi
  • 1,629
  • 15
  • 20
0

std::pair is evaluated at compile time while your defined struct is not evaluated at compile time, so talking about performance std::pair is more way better. So std::pair will prevent the program from being constructed if there's an error.

if you want to use your defined struct at compile time you have to use a templated struct.

N.B.: using template and now in C++20 we talk about concept is a good programming practice.

ic_Engineer
  • 313
  • 3
  • 12
0

As always for performance, you have to measure:

That benchmark shows similar result.

Especially std::map already uses std::pair, so you don't even avoid the corresponding include in that case.

I would go for the more readable/expressive.

Jarod42
  • 203,559
  • 14
  • 181
  • 302