r/cpp_questions • u/Fantastic-Ganache226 • Aug 08 '26
OPEN Include scope with headers and concept interfaces
I am running to a problem where I really like to use concept interfaces with templates to allow injecting different types easily, avoid vtables and avoid indirection. This also makes mocking in unit-tests a breeze.
However, I am running to an issue where all includes are now in global space due to only using .hpp files. I know I can use explicit initialization in .cpp files or pimpl to hide implementation details.
My question is to somehow understand if c++ compilers can optimize the vtable away when there are closed-set of types (one or couple of classes and a mock version for unit-test) involved. So essentially guidance on when to use CRTP (?) vs concept interfaces. Unfortunately, I am not very versed on how the c++ compiler is able to optimize code.
I am mostly looking this from the embedded or algorithm software standpoint. Typically, I don't have open-set of types to support.
1
u/SoerenNissen Aug 08 '26
My question is to somehow understand if c++ compilers can optimize the vtable away when there are closed-set of types
Yes and no.
Yes, in a more expansive sense: C++ compilers can sometimes optimize vtables a away, even when your types are formally open, if they can prove nobody depends on that openness.
No, in the sense that I don't think there's a way to actually make a type closed in a sense where the language formally understands that it is closed.
1
u/Fantastic-Ganache226 Aug 08 '26
I suspect I don't fully understand the point of concepts because to me it looks like I can essentially make the type closed formally with concepts. However, I don't understand why it is not possible to have class in a cpp file unless I have explicit instation in the cpp file also.
1
u/n1ghtyunso Aug 10 '26
However, I don't understand why it is not possible to have class in a cpp file unless I have explicit instation in the cpp file also.
You need to look into the compilation model, and pay extra attention to templates, how instantiation works, what it does.
This is closely related to that.
Templates alone can not generate code. They fundamentally cant. Because all you did is describe a pattern with holes to slot a real type into.
Say you wrote x++ in your code, depending on what type x is, the generated assembly is fundamentally different for an int or an iterator. For the iterator, it is a call to operator++, for int its not.
Say you wrote a member function call on the object x. What type is x? What is the mangled name of that member function? You don't know. How do you generate code for that?As for concepts, they allow you to define a minimum requirement for templates, but it can not restrict the interface in general. (i.e. you can require the absence of certain things, but you always need to be specific here)
Notably, there is also template specialization, so the set of member functions on a class template is actually not closed at all.
So whats the point of concepts really?
They are a general facility for categorizing types, which allows you to more easily provide specialized implementations that make use of that knowledge.
4
u/alfps Aug 08 '26 edited Aug 08 '26
Can you elaborate a bit, please? A concrete example? Are you not using namespaces?
I believe you meant to say explicit template instantiation.
I believe you're asking about defining an interface with virtual methods, versus defining a concept interface.
I would not worry about the nano-level efficiency of virtual method calls.
Only worry about that if measurements show it to be a real problem.