All Questions
234
questions
69
votes
25
answers
30k
views
Are C++ Templates just Macros in disguise?
I've been programming in C++ for a few years, and I've used STL quite a bit and have created my own template classes a few times to see how it's done.
Now I'm trying to integrate templates deeper ...
35
votes
9
answers
27k
views
How do I implement no-op macro (or template) in C++?
How do I implement no-op macro in C++?
#include <iostream>
#ifdef NOOP
#define conditional_noop(x) what goes here?
#else
#define conditional_noop(x) std::cout <&...
28
votes
8
answers
19k
views
Trouble with template parameters used in macros
I'm trying to compile the following piece of code, I get an error on the line which specializes std::vector, it seems the one parameter being passed-in is somehow being assumed to be two parameters. ...
21
votes
1
answer
10k
views
PyBind11 Template Class of Many Types
I'd like to use PyBind11 to wrap a specialized array class. However, the array is available in many flavours (one per each plain-old-datatype). The code looks like this:
py::class_<Array2D<...
19
votes
8
answers
9k
views
How to pass multi-argument templates to macros?
Say I have a macro like this:
#define SET_TYPE_NAME(TYPE, NAME) \
template<typename T> \
std::string name(); \
\
template<>...
13
votes
5
answers
7k
views
Viewing compiler expanded code - C++
I learned that compiler will expand macros while compiling. Templates are also expanded at the compile time. Is there any way to see this expanded code? I am compiling using Visual Studio 2008.
any ...
13
votes
3
answers
7k
views
C++ type registration at compile time trick
I have the following situation: suppose I have a bunch of types (functors) which I want to register/compile in during compilation, preferably into something like boost::mpl::vector.
Do you know any ...
12
votes
3
answers
2k
views
Automate pimpl'ing of C++ classes -- is there an easy way?
Pimpl's are a source of boilerplate in a lot of C++ code. They seem like the kind of thing that a combination of macros, templates, and maybe a little external tool help could solve, but I'm not sure ...
11
votes
3
answers
7k
views
C++ Template preprocessor tool [duplicate]
Is there a compiler or standalone preprocessor which takes C++ files and runs a template expansion pass, generating new C++ code with expanded template instantiations?
I remember such a tool in the ...
10
votes
4
answers
3k
views
What comes first - template instantiation vs. macro expansion?
Let's consider a code example like this (it is just an artificial example to combine define and template, don't look for any sense it it):
#define COMMA ,
template <typename A> class Test
{
...
10
votes
6
answers
631
views
How to implement large number of complex wrappers for legacy API/framework (C++ Macros vs. C++ Templates vs. Code generator)?
We work with very old legacy system implemented in C++ with VC6 compiler. Now we are in the process of refactoring the code. We also switched to VC9 compiler.
We use an external proprietary framework,...
9
votes
2
answers
1k
views
What's the purpose of dummy addition in this "number of elements" macro?
Visual C++ 10 is shipped with stdlib.h that among other things contains this gem:
template <typename _CountofType, size_t _SizeOfArray>
char (*__countof_helper(UNALIGNED _CountofType (&...
9
votes
1
answer
3k
views
C++ template macro shortcut
Often when working with templates, you end up with something like:
template <T>
class the_class
{
public:
// types
typedef T value_type;
typedef const value_type const_value_type;
...
9
votes
1
answer
1k
views
typed vs untyped vs expr vs stmt in templates and macros
I've been lately using templates and macros, but i have to say i have barely found information about these important types. This is my superficial understanding:
typed/expr is something that must ...
9
votes
2
answers
622
views
Similarities and differences between Scala macros and C++ templates [closed]
Scala's Macros and C++ templates both provide access to compile time meta-programming. Could you elaborate on similarities and differences? Are they equal in terms of expressiveness?
9
votes
1
answer
867
views
Macro to get the type of an expression
Question
I am trying to write a C++ macro that would take either type or type name as input, and give type as output.
For example:
REMOVE_NAME(int) should be int
REMOVE_NAME(int aNumber) should also ...
8
votes
3
answers
230
views
How can I pass multiple function overloads as a single argument?
This is another case of trying to get rid of a macro. Consider
void f_(int i) { printf("f_(int)\t%d\n", i); }
void f_(int i, double x) { printf("f_(int, double)\t%d, %5.3f\n", i, x); }
void g_(int i) ...
8
votes
2
answers
4k
views
How to define a template for adding multiple files to a project?
My new project is my first look at WPF MVVM and WCF and I like it but it seems like I am creating a lot of files, always in the same basic setup and structure.
I am wondering if anyone has a way of ...
8
votes
3
answers
2k
views
Automatic Proxy Class
Assume I have an interface
class I{
public:
virtual void f(int id)=0;
virtual void g(int id, float x)=0;
}
I need a proxy class, to do some sort of id to pointer mapping
class Proxy : I
{
...
7
votes
5
answers
3k
views
how to check if function exists
I want to implement my own std::make_unique function with the function being part of std namespace. I know this helper function is added to C++14 but I do not have it in C++11. So, I want to check it ...
7
votes
3
answers
247
views
Can Template / Preprocessor hackery be used to support variable arguments in the middle of an argument list?
I have stumbled upon old code that looks like this:
void dothing(bool testBool,
const std::string& testString1,
const std::string& file,
int line,
...
6
votes
6
answers
5k
views
Detecting a function in C++ at compile time
Is there a way, presumably using templates, macros or a combination of the two, that I can generically apply a function to different classes of objects but have them respond in different ways if they ...
6
votes
2
answers
22k
views
Template in a Macros in C++?
Is it possible to do so
# define abc<T1> __abc<T1, T2>
template<typename T2> void somefun() {
...
abc<int>(...);
abc<double>(...);
...
}
Just to not ...
6
votes
2
answers
226
views
What does this C++ template macro mean?
Can somebody please break down this for me? I know macros, and I am fairly familiar with templates, but I have no idea what the author is expressing with this. What's the intended usage, why is it ...
6
votes
1
answer
1k
views
ReSharper template macro for method return type
I can't seem to find a way to insert the method return type in a ReSharper 5 macro. Can this be done?
For example, I have the following template:
Contract.Ensure(Contract.Result<$RESULTTYPE$>() !...
6
votes
3
answers
2k
views
Is there a way to generate multiple files through one template/macro with Resharper?
When I write my APIs for applications I always follow a set pattern (who doesnt!?) that looks like this:
User (Folder/Namespace)
User.cs
UserDao.cs
UserDaoFactory.cs
UserService.cs
...
6
votes
2
answers
208
views
reducing syntax "noise" without using a macro
I'm trying to find a way of reducing a bit of syntax "noise" without resorting to a macro. For the following code:
struct base { base() = delete; };
struct tag1 final : private base
{
static ...
6
votes
2
answers
9k
views
Jinja2: saying 'Render this macro inside another macro or template'
I have a number of macros I'd like to render within a common container macro or template. With pseudo-code:
Macro1
Macro2
Macro3
Container
In a template:
"render macro1 inside of Container" e.g. ...
6
votes
1
answer
5k
views
Scalable automatic class registration in C++
Automatic class registration in C++ is a common task, and a commonly asked question here on StackOverflow:
Register an object creator in object factory
Somehow register my classes in a list
...
5
votes
3
answers
9k
views
How to check if a member name (variable or function) exists in a class, with or without specifying type? [duplicate]
This Q is an extension of:
Templated check for the existence of a class member function?
Is there any utility which will help to find:
If a member name exists inside a class or not? This member can ...
5
votes
4
answers
2k
views
C++ construct that behaves like the __COUNTER__ macro [duplicate]
I have a set of C++ classes and each one must declare a unique sequential id as a compile-time constant.
For that I'm using the __COUNTER__ built-in macro which translates to an integer that is ...
5
votes
1
answer
1k
views
macro expansion with templates
I'm writing several functions which takes as input the result of a template function:
int alg1(Vect3) {...}
...
int algN(Vect3) {...}
void main() {
alg1( mat.topRightCorner<3,1>() )
}
...
5
votes
2
answers
11k
views
automatic registration of object creator function with a macro
Basically, I want to automatically register object creator functions with an object factory for a bunch of classes defined across many header files.
The top answer to this post, provides a solution --...
5
votes
2
answers
380
views
Derived Class with no overhead using templates?
I am trying to accomplish the following:
Object.
Debug version of object with extra functionality in functions for tracing purposes.
Now, I currently have a compile-time solution using macros, which ...
5
votes
2
answers
3k
views
How to render a Jinja2 macro from a view callable?
I have some macros defined that are called from several templates.
For example, the Product page has a Review section that uses the macros defined in 'helpers/review.jinja2' to print each review. The ...
5
votes
3
answers
2k
views
How could my code tell a compile-time constant versus a variable?
Here's my problem. I have a BINARY_FLAG macro:
#define BINARY_FLAG( n ) ( static_cast<DWORD>( 1 << ( n ) ) )
Which can be used either like this ("constant" scenario):
static const ...
5
votes
1
answer
2k
views
Passing template argument commas through macros safely?
I'm currently using macros to declare relatively long lists of partially specialized template classes with better brevity. Somewhat oversimplified examples follow:
#define INSTANTIATE_MYTYPE(...
5
votes
3
answers
553
views
c++, a generic recursive template function to traverse tree like structures
I tried to traverse tree like structures with a generic recursive function without defining a recursive function in global each time for each structures.
//structure #1
class CA
{
public:
std::...
5
votes
1
answer
1k
views
Doxygen for C Template Emulation
I am trying to generate documentation using Doxygen for emulated templates in C without much success. I am hoping someone knows how to make the macro trickery work in the doxygen preprocessor? I have ...
4
votes
3
answers
922
views
Checking for empty for loops in C++
Is it possible to check for empty for loops in C++, perhaps some clever template/macro trick? Seems like GCC does not support -Wempty-body for for loops.
4
votes
3
answers
5k
views
How to extract __VA_ARGS__?
I hava a macro to call static function for each args.
For example:
#define FOO(X) X::do();
#define FOO_1(X,Y) X::do(); Y::do();
My question is that I need to use foo with variable number of ...
4
votes
2
answers
2k
views
C# macro-style generics
I am looking for a way to have generics which only exist at compile time for the purpose of code reuse: not having to copy/paste classes and methods. Just text replacement basically, like macros, with ...
4
votes
1
answer
250
views
BOOST_FUSION_ADAPT_TPL_STRUCT with template members
I am new to StackOverflow and new to writing macro in c++, please forgive me if this question is too naive.
I have written a template class like this:
template<typename T, typename U>
...
4
votes
1
answer
2k
views
C++ Constant preprocessor macros and templates
So say I have the following very simple macro, along with a bit of code to output it:
#define SIMPLEHASH(STRING) STRING[1] + STRING[2] + STRING[3]
std::cout << SIMPLEHASH("Blah");
This ...
4
votes
1
answer
131
views
Write a function which will automatically determine its return value by judging the type of each of its parameters
The max() function in the <algorithm> header doesn't support two parameters of different datatypes. So I tried to write my own code using template and #if, #else and #endif macros. My code so ...
4
votes
3
answers
2k
views
How to replace this macro with inheritance, or template (or anything else?)
My question is similar to: Can't use macro define class in C++, but is a little more complicated:
class ABC
{
public:
DECLARATION(ABC)
private:
void ABCFun1();
void ABCFun2();
// ...
4
votes
1
answer
200
views
Template function in define
I have some template function and I want to call it using define in C++:
#define CONFIG(key, type, def) getValue<type>(key, def);
Of course, it won't work. Could I make something like this?
4
votes
2
answers
693
views
Wrapping a template function call in a macro, or avoid specializing a void return
I have the following function that allows me to wrap OpenGL commands and log when something goes wrong:
template<typename Res, typename Func, typename... Args>
struct Checker {
static Res ...
3
votes
2
answers
2k
views
Calling different C functions according to the C++ template type
my problem is the following:
I have a C library which contain several versions of each function according to which data type they are working with e.g.:
void add(double *a, double *b, double *c);
...
3
votes
2
answers
2k
views
Template function with macro - accumulate on vector
I want to create a function that get vector<int> run over all his elements and "sum" them according to specific operator I chose .
For example , v1 = [3,6,7] so I could calculate by this ...