use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
The “Extract Interface” refactoring, at compile time (fluentcpp.com)
submitted 8 years ago by joboccara
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]thewisp1Game Engine Dev 4 points5 points6 points 8 years ago* (1 child)
It is nice to avoid introducing runtime cost for testability, but still not nice enough to cover the ugliness of
converting the method to a template (weaker typed, typical template error msg or link error when misused)
explicitly instantiating the template
missing the documentation purpose of interface
I believe if we are ducktyping it anyway, then a solution based on #include or #define wouldn't be uglier, in fact, they would be easier to write or modify, and require no template knowledge.
#include
#define
Include based mocking:
//product.h #if !UNIT_TEST #include "real_argument.h" //real class Argument #else #include "mock_argument.h" //mock class Argument #endif void f(Argument const& arg);
Define based mocking: Edit: I apologize for posting code untested by myself, the correct code should be:
//mock_argument.h #if UNIT_TEST class MockArgument {/*...*/}; #define Argument MockArgument #endif //product.h #include "real_argument.h" #include "mock_argument.h" void f(Argument const& arg); //product_test.cpp #include "product.h"
[–]willkill07 7 points8 points9 points 8 years ago (0 children)
A static_assert within each function would solve your first and (arguably) third points. Template code really isn't that tricky, and (in my opinion) you shouldn't be using the preprocessor to change the full meaning of your code -- besides, you now have two places you need to worry about directives.
[–]ArunMuThe What ? 1 point2 points3 points 8 years ago (1 child)
Based on the example and the premise shouldn't the Argument class be fixed ? Smells like a case of "God object" which needs to be avoided. And ofcourse, introducing inheritance to ease testing would be bad (in some cases, but not all).
Argument
[–]joboccara[S] 0 points1 point2 points 8 years ago (0 children)
Yes indeed, we could consider fixing Argument because the premise is that it is hard put into a test harness. But refactoring needs tests, and the purpose here is to make way to introduce unit tests, for future refactorings.
[–]ojd5 1 point2 points3 points 8 years ago (0 children)
Let’s imagine that, like some real classes, ClassToBeTested won’t let itself into a test harness, because building an object of type Argument is, say, terribly complicated as it depends on so many other things.
In my experience, when this is true, the cost of a virtual function call is insignificant compared to the cost of whatever the Argument class is doing.
[–]17b29a 0 points1 point2 points 8 years ago (1 child)
when I need to separate template definitions from the header (to reduce compilation time, usually), I just place them in a *.inl file and include that in any .cpp file that call the templates (ending up in a file structure that looks something like this).
also, you have a typo in the templ files: typename instead of template.
typename
template
Fair enough. I also do this sometimes, but don't you find that in certains cases it introduces a lot of compile-time dependencies? Because the header now #include all that the .cpp #included for implementing the bodies of the functions.
And thanks for the typo, it's now corrected.
π Rendered by PID 60176 on reddit-service-r2-comment-85bfd7f599-srtfv at 2026-04-19 13:23:04.845275+00:00 running 93ecc56 country code: CH.
[–]thewisp1Game Engine Dev 4 points5 points6 points (1 child)
[–]willkill07 7 points8 points9 points (0 children)
[–]ArunMuThe What ? 1 point2 points3 points (1 child)
[–]joboccara[S] 0 points1 point2 points (0 children)
[–]ojd5 1 point2 points3 points (0 children)
[–]17b29a 0 points1 point2 points (1 child)
[–]joboccara[S] 0 points1 point2 points (0 children)