1

Is there good pratices to declare a using (for typedef like usage) ?

For example if I have a class with a std::array<std::array<T, W>, H> member and I want to ease both reading and writing with a using like this

template<typename T, uint32_t H, uint32_t W>
using matrix = std::array<std::array<T, W>, H>;

Where should I put this ? Inside the class declaration or outside or even in a separate header file ?

Thanks

Christophe
  • 68,716
  • 7
  • 72
  • 138
Pezzza
  • 83
  • 1
  • 7
  • 1
    In my opinion it's best to do so in the closest scope of usage, e.g. in a single translation unit, or may be even inside a single function definition. – πάντα ῥεῖ Mar 24 '19 at 09:09
  • 2
    Header files should usually not have `using` in their global scope. Especially not `using namespace std;` kind – Aykhan Hagverdili Mar 24 '19 at 09:15
  • 2
    Related: https://stackoverflow.com/questions/2356548/header-file-best-practices-for-typedefs https://stackoverflow.com/questions/759512/internal-typedefs-in-c-good-style-or-bad-style – πάντα ῥεῖ Mar 24 '19 at 09:38
  • Thank you very much for your comments. πάντα-ῥεῖ indeed it is really similar, I will check this – Pezzza Mar 24 '19 at 09:44
  • This depends on too many things. Are you only using it in one place? One class? One function? The entire project? Your project is a library and you want to expose the type for other people to use? Etc... – Galik Mar 24 '19 at 10:40
  • If you put it anywhere global, put it is a namespace (along with all your other related symbols). Generally for anything non trivial, put anything than can go in a namespace in a namespace. – Galik Mar 24 '19 at 10:42
  • @Ayxan the `using` mantra regarding header only applies to `using namespace`. Here the `using` is for type alias. As long as class definition and typedefs are a reasonable practice in headers, so are type alias `using` statements. – Christophe Mar 24 '19 at 11:21
  • @Christophe thanks for pointing out. Yes, type aliases are usually alright and are widely used in headers. But `using std::string` and `using namespace std` and such are generally avoided in headers. – Aykhan Hagverdili Mar 24 '19 at 11:31

1 Answers1

3

Your type alias template has exactly the same purpose than a template class, i.e. define a type:

template<typename T, uint32_t H, uint32_t W>
using matrix = std::array<std::array<T, W>, H>;

int main() {
    matrix<double, 10,3> m; 
    return 0;
}

So the good practice would be to handle it exactly as you would do with other type definitions:

  • Put it in a header if you intend to reuse this definition in many places (and it seems so for your example, since a matrix is something rather general);
  • Embed it in a class (probably in a header) if it's an implementation detail that has not a general purpose;
  • Put it in the compilation unit where you use it, if you use your matrix in a single source file.

For non-template type alias, it's the same principle as with the old typedef, so exactly the same as above, and in addition,

  • Put it in a function body, if it has the sole purpose of serving as a very local shortcut for a very long type name.
Christophe
  • 68,716
  • 7
  • 72
  • 138
  • Ish. It's _not_ defining a new type, actually, (any more than any other template instantiation does, at least) so you should be careful of weird edge cases and thus perhaps consider making the type's name scoped more tightly than you'd otherwise bother doing. Though off the top of my head I can't think of any serious gotchas that warrant giving this too much thought. Come to think of it, _because_ it's not a new type, maybe there aren't any edge cases. Meh. – Lightness Races in Orbit Mar 24 '19 at 11:49
  • @LightnessRacesinOrbit you are of course completely right. It doesn't *define* a new type in the sense of the C++ standard terminology, it only *declares* an alias name for an existing type. I was thinking of using the term "providing" to avoid any confusion, but this would have raised other questions and ambiguities. In the end I preferred to keep the term "defining" in its less precise common-sense, since the question is *where* to put them rather than *what* they are or *how* they could be used. – Christophe Mar 24 '19 at 12:13
  • Yeah it's fine as-is - my intention was only to add discussion about possible caveats, but by the time I was done I think I decided there aren't any – Lightness Races in Orbit Mar 24 '19 at 12:25