1

I hate the boilerplate and Don't Repeat Yourself violations inherent in traditional class declarations in C++.

Is it possible to create a template with no template parameters, purely to enable the class to be defined in a header file without violating the One Definition Rule, in C++11?

Andrew Wagner
  • 22,677
  • 21
  • 86
  • 100
  • what would be the point? If what you're wanting is to declare & implement a class entirely in the header, you can do that without it being a template. Note however that every module that includes that header will get a copy of that implementation and you may get "duplicate symbol" linker errors. – GreatAndPowerfulOz Oct 29 '15 at 15:24
  • 3
    this is called a [header only library](https://en.wikipedia.org/wiki/Header-only) and there are [no templates required](http://stackoverflow.com/questions/16441036/when-using-a-header-only-in-c-c). – m.s. Oct 29 '15 at 15:26
  • 2
    I think the One Definition Rule is not as restrictive as you seem to think. – Chris Beck Oct 29 '15 at 15:26
  • 2
    Is writing `inline` really such a burden? – Barry Oct 29 '15 at 15:26
  • 1
    See the accepted answer to [this question](http://stackoverflow.com/questions/1759300/when-should-i-write-the-keyword-inline-for-a-function-method?lq=1). Best explanation I've ever seen anywhere. – Andy Brown Oct 29 '15 at 15:32
  • I'm still learning about how classes affect linkage, figuring out what the status quo is, and deciding whether or not to fight it by writing in a functional style, i.e. returning tuples to mimic patterns from Rust and Haskell. – Andrew Wagner Oct 29 '15 at 15:47
  • @AndrewWagner, good luck with that. You might be better off just really learning C++ really well and do things the C++ way. Just a thought. – GreatAndPowerfulOz Oct 29 '15 at 16:19

2 Answers2

2

There's no need for templates whatsoever.

If you want to write a header-only class, all you have to do is mark inline the functions that will be defined external to the class declaration:

#pragma once

struct some_class {
    void implicitly_inline() { ... }

    inline void explicitly_inline();
};

void some_class::explicitly_inline() { ... }

The occasional extra inline keyword is hardly such a burden as to change the entire definition of your class.

m.s.
  • 16,063
  • 7
  • 53
  • 88
Barry
  • 286,269
  • 29
  • 621
  • 977
2

Is it possible to create a template with no template parameters

No. And you don't need for such workaround because...

to enable the class to be defined in a header file without violating the One Definition Rule

You can define a class in a header file without violating the One Definition Rule.

You can even define the member functions of a class in a header - which I think is the point of this question. Simply declare them all inline. If you define the member functions within the class definition, then they're implicitly inline.

There may be more than one definition of an inline function in the program as long as each definition appears in a different translation unit. For example, an inline function may be defined in a header file that is #include'd in multiple source files.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • OK, so if I understand correctly, the re-compile my class every time I #include it, but the linker will merge the redundant instances? And I should pray that my build is configured properly so that old .o files don't stick around... – Andrew Wagner Oct 29 '15 at 15:42
  • @AndrewWagner A compiler may skip re-compiling if it has such optimization, but in principle, yes. There are similarities in linking templates inlines. – eerorika Oct 29 '15 at 15:48