0

I want to know whether it is possible to do generic programming in C++ without using templates. Is it possible to write all libraries available in C++ which are written using templates without using templates. Is there any alternative available in C++ for templates?

I want to know Is it possible to write an abstraction over libraries written in C++ using templates which provide me with the same functionality

sv_jan5
  • 1,543
  • 16
  • 42
  • 2
    Given that most of the standard library consists of templates, no. It is like asking whether you can make scrambled eggs without eggs. – juanchopanza Apr 05 '16 at 06:47
  • Hypothetically, you can follow the Java approach and make everything a subclass/descendant of a generic parent `Object` class or `CObject` class. Practically, it's a different story. – thor Apr 05 '16 at 06:49
  • @tinlyx But that would not give you the same functionality. – juanchopanza Apr 05 '16 at 06:50
  • What is the point of this question? If template is useless in C++ (or C++ without template is equivalent to C++ with template), it won't be in C++ at first place. – Bryan Chen Apr 05 '16 at 06:51
  • I want to know Is it possible to write an abstraction over libraries written in C++ using templates which provide me with same functionality? – sv_jan5 Apr 05 '16 at 07:05
  • @sv_jan5 No, because some of the libraries are *about* templates. Maybe you need to narrow the scope of your question down. – juanchopanza Apr 05 '16 at 07:09
  • 2
    I'm voting to close this question as off-topic because it's a silly question. – Richard Hodges Apr 05 '16 at 07:23
  • You can write C++ without using the letter "e". That's not sensible either. – MSalters Apr 05 '16 at 07:25
  • Why not go the whole way and ommit inheritance and polymorphism? Away with all that `for` and `while`! `if` and `goto` will do. But seriously: Why, why ever for heaven's sake would you want to use C++ without templates? – TobiMcNamobi Apr 05 '16 at 08:29
  • @TobiMcNamobi I am working on a compiler which generates C++ code. But my language doesn't support templates that's why I am searching for alternatives – sv_jan5 Apr 05 '16 at 09:11
  • 1
    @sv_jan5 Do I understand this correctly: You are designing some language and the parser generates C++ code. It never generates templates though. Every modern C++ compiler (and almost all not so modern ones btw) will compile this. Where's the problem? – TobiMcNamobi Apr 05 '16 at 09:18
  • @TobiMcNamobi Problem is that if I want to support modern libraries in my language which are written in C++ using templates. I am trying to find a way to develop an abstraction over such libraries so that I could provide them in my language. – sv_jan5 Apr 06 '16 at 03:47

3 Answers3

2

Theoretically, C++ without templates is still Turing-complete, so you can write a program for every function in that language that can also be written in C++ with templates. To my knowledge, the macro preprocessor in C++ is not Turing-complete, but templates are. So there must exist functions which can be implemented purely as templates, but not with macros.

Practically, I don't think it is possible to re-implement everything with the same semantics. Without templates, you probably will have to sacrifice type-safety and stick to using macros, void* or inheritance-based approaches like the early Java classes did even for simple container libraries.

For more advanced meta-programming libraries, e.g. expression templates, dimensional analysis frameworks, Boost.Spirit Boost.Proto, I doubt that they can be implemented without another form of meta-programming. Macros may work, but this will be more like a code-generator and defer type-checking to the compiler and error messages will be even worse than what we have right now with templates. In addition, the semantics are different w.r.t parameter passing.

Jens
  • 9,058
  • 2
  • 26
  • 43
  • Why does your first sentence say "Theoretically", as if writing complex C++ programs without templates is something difficult that hasn't been thoroughly tested many times? I mean, C exists. – Benjamin Lindley Apr 05 '16 at 08:35
  • I'd like to add that templates like macros are a kind of code-generator. As Jens mentioned the difference is *when* the code is generated. Pre-processing vs compilation, text manipulation vs type-checking. So templates are the superior code generators. – TobiMcNamobi Apr 05 '16 at 08:37
  • 1
    @BenjaminLindley I said theoretically because it a argument based on computational theory. I wasn't referring to computational expressiveness or if this is useful or not, although I do think that generic data types are a huge improvement. – Jens Apr 05 '16 at 11:26
1

Well, templates are just that — templates. They are blueprints for actual types and functions. So, theoretically, you can make all of those template instantiations by hand. But that wouldn't be generic programming any more.

grisumbras
  • 850
  • 2
  • 6
  • 11
  • 1
    Templates are a little bit more than that, at least in C++. They are a Turing-complete language. – Jens Apr 05 '16 at 07:13
1

Answer for question:

Is there any alternative available in C++ for templates?

Macroses is alternative of templates. (Not good alternative, but alternative) Related links [1],[2]

Compare:

#define min(i, j) (((i) < (j)) ? (i) : (j))

template<class T> T min (T i, T j) { return ((i < j) ? i : j) }

Problems of macroses:

  • no type checking,
  • not so understandable compiler errors becouse macroses expanding
  • sideffects of multiple computing expressions

About question:

Is it possible to write all libraries available in C++ which are written using templates without using templates.

It is possible to use macroses in some cases. It is possible to write or generate implementation for each library type. But for user defined type library can not have implementation, except simple cases when macroses may be useful. Earlier pure C (not C++) programs have contain special tools in sources that was used at build stage to generate some sources from some "presource" templates.

Community
  • 1
  • 1
oklas
  • 7,935
  • 2
  • 26
  • 42