Consider the following code:
typedef float (*MathsOperation)(float _1, float _2);
struct Data
{
float x, y;
MathsOperation op;
};
Data data[100];
float Add(float _1, float _2){//add}
float Sub(float _1, float _2){//subtract}
float Mul(float _1, float _2){//multiply}
// other maths operations
for (int i = 0; i < 100; ++i)
{
// assign one of the maths operators above to data struct member op
// according to some condition (maybe some user input):
if(condition1) data[i].op = &Add;
if(condition2) data[i].op = ⋐
if(condition3) data[i].op = &Mul;
// etc.
}
Now I'd like to somehow save the dataarray to a file and load it later (maybe in another program which doesn't know about the conditions that were used to assign the operators to each array element). Obviously, the pointers would be different every time I ran the application. So, my question is what is the best way to do this?
You can't store "functions" as data anyway, and as you say, storing pointers in external media doesn't work. So, what you have to do in this case is store an operator value, e.g.
enum Operator
{
Op_Add,
Op_Sub,
Op_Mul,
Op_Largest // For array size below.
};
And instead of:
if(condition1) data[i].op = &Add;
if(condition2) data[i].op = ⋐
if(condition3) data[i].op = &Mul;
have:
if(condition1) data[i].op = Op_Add;
if(condition2) data[i].op = Op_Sub;
if(condition3) data[i].op = Op_Mul;
Since that is an integer type value, it can be stored in a file, and you can then do:
// Or `fin.read(reinterpret_cast<char*>(data), sizeof(data))
fin >> op >> x >> y;
if (op == Op_Add) ...
else if (op == Op_Sub) ...
Or have a function pointer array that you index with op... In other words:
typedef float (*MathsOperation)(float _1, float _2);
...
MathsOperation mathsOps[Op_Largest] = { &Add, &Sub, &Mul };
...
mathsOps[op](x, y);
...
If I where you I would build an index, where you could register your operators
static std::array<MathsOperation> MathsOperations;
MathsOperations.push_back(Add);
MathsOperations.push_back(Sub);
MathsOperations.push_back(Mul);
int getIdx(MathsOperation op) {
return std::find(MathsOperations.begin(), MathsOperations.end(), op) - MathsOperations.begin();
}
and put it in a .h file just after the MathsOperation definitions
Then rather then saving the function pointer, you could just save the relevant index and access the operator afterwards
int opidx = getIdx(Add);
MathsOperator op = MathsOperator[idx];
Non-portable, but almost certain to work if all your functions are in the same module:
template<typename FuncT>
intptr_t FunctionPointerToId( FuncT* fptr )
{
return reinterpret_cast<intptr_t>(fptr) - reinterpret_cast<intptr_t>(&Add);
}
template<typename FuncT>
FuncT* FunctionPointerFromId( intptr_t id )
{
return reinterpret_cast<FuncT*>(i + reinterpret_cast<intptr_t>(&Add));
}
This assumes that your implementation preserves relative addresses of functions within the same module (most platforms do guarantee this as implementation-specific behavior, since dynamic loaders rely on this). Using relative addresses (aka "based pointers") allows it to still work even if the module is a shared library that gets loaded at a different base address each time (e.g. ASLR).
Don't try this if your functions come from multiple modules, though.
If you have the ability to build and maintain a list of the functions, storing an index into that list is definitely a better approach (those indexes can remain good even after relinking, while relative code addresses get changed).
You need some permanent identifier for each function. You save this identifier instead of function address and restore address after reading.
The simpliest is the integer identifier which is an index of array
const MathsOperation Operations[] = { &Add, &Sub };
In this case you must never change the order of Operations items.
If it is impossible, use strings:
const std::map<std::string, MathsOperation> OpNames
{
{ "Add", &Add },
{ "Sub", &Sub },
};
Related
Guys I have a function like this (this is given and should not be modified).
void readData(int &ID, void*&data, bool &mybool) {
if(mybool)
{
std::string a = "bla";
std::string* ptrToString = &a;
data = ptrToString;
}
else
{
int b = 9;
int* ptrToint = &b;
data = ptrToint;
}
}
So I want to use this function in a loop and save the returned function parameters in a vector (for each iteration).
To do so, I wrote the following struct:
template<typename T>
struct dataStruct {
int id;
T** data; //I first has void** data, but would not be better to
// have the type? instead of converting myData back
// to void* ?
bool mybool;
};
my main.cpp then look like this:
int main()
{
void* myData = nullptr;
std::vector<dataStruct> vec; // this line also doesn't compile. it need the typename
bool bb = false;
for(int id = 1 ; id < 5; id++) {
if (id%2) { bb = true; }
readData(id, myData, bb); //after this line myData point to a string
vec.push_back(id, &myData<?>); //how can I set the template param to be the type myData point to?
}
}
Or is there a better way to do that without template? I used c++11 (I can't use c++14)
The function that you say cannot be modified, i.e. readData() is the one that should alert you!
It causes Undefined Behavior, since the pointers are set to local variables, which means that when the function terminates, then these pointers will be dangling pointers.
Let us leave aside the shenanigans of the readData function for now under the assumption that it was just for the sake of the example (and does not produce UB in your real use case).
You cannot directly store values with different (static) types in a std::vector. Notably, dataStruct<int> and dataStruct<std::string> are completely unrelated types, you cannot store them in the same vector as-is.
Your problem boils down to "I have data that is given to me in a type-unsafe manner and want to eventually get type-safe access to it". The solution to this is to create a data structure that your type-unsafe data is parsed into. For example, it seems that you inteded for your example data to have structure in the sense that there are pairs of int and std::string (note that your id%2 is not doing that because the else is missing and the bool is never set to false again, but I guess you wanted it to alternate).
So let's turn that bunch of void* into structured data:
std::pair<int, std::string> readPair(int pairIndex)
{
void* ptr;
std::pair<int, std::string> ret;
// Copying data here.
readData(2 * pairIndex + 1, ptr, false);
ret.first = *reinterpret_cast<int*>(ptr);
readData(2 * pairIndex + 2, ptr, true);
ret.second = *reinterpret_cast<std::string*>(ptr);
}
void main()
{
std::vector<std::pair<int, std::string>> parsedData;
parsedData.push_back(readPair(0));
parsedData.push_back(readPair(1));
}
Demo
(I removed the references from the readData() signature for brevity - you get the same effect by storing the temporary expressions in variables.)
Generally speaking: Whatever relation between id and the expected data type is should just be turned into the data structure - otherwise you can only reason about the type of your data entries when you know both the current ID and this relation, which is exactly something you should encapsulate in a data structure.
Your readData isn't a useful function. Any attempt at using what it produces gives undefined behavior.
Yes, it's possible to do roughly what you're asking for without a template. To do it meaningfully, you have a couple of choices. The "old school" way would be to store the data in a tagged union:
struct tagged_data {
enum { T_INT, T_STR } tag;
union {
int x;
char *y;
} data;
};
This lets you store either a string or an int, and you set the tag to tell you which one a particular tagged_data item contains. Then (crucially) when you store a string into it, you dynamically allocate the data it points at, so it will remain valid until you explicitly free the data.
Unfortunately, (at least if memory serves) C++11 doesn't support storing non-POD types in a union, so if you went this route, you'd have to use a char * as above, not an actual std::string.
One way to remove (most of) those limitations is to use an inheritance-based model:
class Data {
public:
virtual ~Data() { }
};
class StringData : public Data {
std::string content;
public:
StringData(std::string const &init) : content(init) {}
};
class IntData : public Data {
int content;
public:
IntData(std::string const &init) : content(init) {}
};
This is somewhat incomplete, but I think probably enough to give the general idea--you'd have an array (or vector) of pointers to the base class. To insert data, you'd create a StringData or IntData object (allocating it dynamically) and then store its address into the collection of Data *. When you need to get one back, you use dynamic_cast (among other things) to figure out which one it started as, and get back to that type safely. All somewhat ugly, but it does work.
Even with C++11, you can use a template-based solution. For example, Boost::variant, can do this job quite nicely. This will provide an overloaded constructor and value semantics, so you could do something like:
boost::variant<int, std::string> some_object("input string");
In other words, it's pretty what you'd get if you spent the time and effort necessary to finish the inheritance-based code outlined above--except that it's dramatically cleaner, since it gets rid of the requirement to store a pointer to the base class, use dynamic_cast to retrieve an object of the correct type, and so on. In short, it's the right solution to the problem (until/unless you can upgrade to a newer compiler, and use std::variant instead).
Apart from the problem in given code described in comments/replies.
I am trying to answer your question
vec.push_back(id, &myData<?>); //how can I set the template param to be the type myData point to?
Before that you need to modify vec definition as following
vector<dataStruct<void>> vec;
Now you can simple push element in vector
vec.push_back({id, &mydata, bb});
i have tried to modify your code so that it can work
#include<iostream>
#include<vector>
using namespace std;
template<typename T>
struct dataStruct
{
int id;
T** data;
bool mybool;
};
void readData(int &ID, void*& data, bool& mybool)
{
if (mybool)
{
data = new string("bla");
}
else
{
int b = 0;
data = &b;
}
}
int main ()
{
void* mydata = nullptr;
vector<dataStruct<void>> vec;
bool bb = false;
for (int id = 0; id < 5; id++)
{
if (id%2) bb = true;
readData(id, mydata, bb);
vec.push_back({id, &mydata, bb});
}
}
I have some C code where I need to do some calculations with an array of data. The data can be either INT or DOUBLE. In order to deal with the different data types, I was thinking of using an if / else statement and define the pointer holding the data inside that statement:
/* put values into M, depending on data type*/
if (data_type == 2)
{
double *M;
M = somefunction(DOUBLE);
} else {
unsigned int *M;
M = somefunction(UINT16);
}
/* dummy code - usually I do some calculations on M which are data type independent */
for (i=0;i<(10);i++) {
M[i]=0;
}
This leads to scoping problems because M is not defined outside the if / else construct:
error: ‘M’ undeclared (first use in this function)
If I move the definition of M outside the if / else statement, the code will compile but M inside the if / else is a different M outside.
So I can circumvent the problem by defining two pointers, one double and one int and check everywhere in my code which type I'm dealing with:
double *Mdouble;
unsigned int *Mint;
/* put values into M, depending on data type*/
if (data_type == 2)
{
Mdouble = somefunction(DOUBLE);
} else {
Mint = somefunction(UINT16);
}
/* dummy code - usually I do some calculations on M which are data type independent */
for (i=0;i<(10);i++) {
if (data_type == 2) {
Mdouble[i]=0;
} else {
Mint[i]=0;
}
}
So here's my question:
How can I solve this problem where M is a double or int, depending on my incoming data? Could I solve this with some kind of pointer to a pointer work around? I don't want to write duplicate code for each case.
EDIT could template functions or overloading of functions solve my problem? I'm flexible regarding a C / C++ specific solution
You're going to have to write duplicate code.
At the base level, the required machine code for adding two numbers is different for integers vs floating-point numbers.
This difference is "hidden" since data is typed, so the compiler always knows the type of each operand and can generate the proper code.
If you want to move that information until run-time, the compiler can no longer do its thing, so you're going to have to do it instead and make sure your code takes the proper path.
You can try to use an C union:
#include <string.h>
#include <stdio.h>
int main(int argc, char** argv) {
union MyUnion {
int I;
double D;
};
union MyUnion M[10];
// Initializing the array to 0 (data type independent)
memset(M, 0, 10*sizeof(MyUnion));
M[0].I = 1;
M[1].D = 1.5;
if (argc==1) { // It should be "if (data_type==2) {"
// but I wanted an example that compiles easily
printf("%i\n", M[0].I); //somefunction(M[0].I);
} else {
printf("%lf\n", M[1].D); //somefunction(M[1].D);
}
}
In C you will have to use a macro
int8_t* ptr=...;
while(n)
{
switch(data_type)
{
case TYPE_DOUBLE:
A_MACRO_THAT_DEFINES_YOUR_OPERATION((double*)ptr);
ptr+=sizeof(double);
break;
case TYPE_INT:
A_MACRO_THAT_DEFINES_YOUR_OPERATION((int*)ptr);
ptr+=sizeof(int);
break;
}
--n;
}
This solution is slow since it needs to test the data type for each element. You can instead write the entire loop as a macro. This will be faster but harder to read.
If you can use C++, use a template instead. Here is a solution using templates:
template<class T>
void doStuff(T* ptr_begin,T* end)
{
while(ptr_begin!=ptr_end)
{
// Do stuff
++ptr_begin;
}
}
void doStuffWrapper(void* ptr_begin,void* ptr_end,uint32_t type)
{
switch(type)
{
case TYPE_DOUBLE:
doStuff((double*)ptr_begin,(double*)ptr_end);
break;
case TYPE_INT:
doStuff((int*)ptr_begin,(int*)ptr_end);
break;
}
}
As a side note: I prefer switch-case over if-else in this case since it is easier to maintain, and may produce faster code.
You can use a macro, e.g.:
#define CALCULATIONS(M) do { \
for (i=0;i<(10);i++) \
(M)[i]=0; \
} while (0)
and then in your other code:
if ( data_type == 2 )
{
double *M = whatever;
CALCULATIONS(M);
}
See here if you are unfamiliar with the do...while(0) technique
It sounds like you want a templatized function.
But I think the question comes down to, can you put all your work in the function?
template <typename T>
T* somefunction(T data){
//start old somefunction code
T* result = new T[10]{data, data, data, data, data, data, data, data, data, data};
//end old somefunction code
//begin "calculations on M which are data type independent"
for(int i = 0; i < 10; i++){
M[i] = 0;
}
return M;
}
So with the above code you could do either double* Mdouble = somefunction(13.13) or short* Mshort = somefunction(13);
The best case scenario is in which M was really a throw away array anyway, in which case you could even allocate the data in the template function's stack frame and avoid using new.
I have a C++ class containing a bunch of data members of the same type and I want to iterate over them:
// C.h
class C {
// other members
double foo;
double bar;
...
double barf; // 57th double declared since foo, nothing else in between
// other members
};
Pointer arithmetic seems to work, e.g. here using the constructor to initialize those 58 member doubles:
// C.cpp
C::C() {
for (int i = 0; i < 58; i++) {
*(&this->foo + i) = 0;
}
}
I found related questions here How to iterate through variable members of a class C++, here C++: Iterating through all of an object's members?, here Are class members garaunteed to be contiguous in memory? and here Class contiguous data, with some people suggesting this kind of thing is ok and others a no-no. The latter say there's no guarantee it won't fail, but don't cite any instances of it actually failing. So my question is, does anyone else use this, or has tried and got into trouble?
Or maybe there's a better way? Originally in my application I did actually use an array instead to represent my object, with indices like so:
int i_foo = 0, i_bar = 1, ..., i_barf = 57;
However once I introduced different objects (and arrays thereof) the index naming started to get out of hand. Plus I wanted to learn about classes and I'm hoping some of the other functionality will prove useful down the line ;-)
I use the iteration pretty heavily, e.g. to calculate statistics for collections of objects. Of course I could create a function to map the class members to an array one-by-one, but performance is a priority. I'm developing this application for myself to use on Windows with VS. I would like to keep other platform options open, but it's not something I intend to distribute widely. Thanks
George:
I think you can have a better solution (like a method that will return the i-th attribute:
double get(size_t idx)
{
switch (idx)
{
case 0: return foo;
case 1: return bar;
case 2: return foo_bar;
....
}
}
Using pointer arithmetic to iterate over class data members can cause problems during code optimization. Example:
struct Vec3
{
double x, y, z;
inline Vec3& operator =(const Vec3& that)
{
x = that.x;
y = that.y;
z = that.z;
return *this;
}
inline double& operator [](int index)
{
return (&x)[index];
}
};
...
Vec3 foo = bar; // operator =
double result = foo[2]; // operator []
...
Both operators are inlined, the value of the result depends on the final instructions reordering. Possible cases:
foo.x = bar.x;
foo.y = bar.y;
foo.z = bar.z;
result = (&foo.x)[2]; // correct -- result contains new value
foo.x = bar.x;
foo.y = bar.y;
result = (&foo.x)[2]; // incorrect -- result contains old value
foo.z = bar.z;
foo.x = bar.x;
result = (&foo.x)[2]; // incorrect -- result contains old value
foo.y = bar.y;
foo.z = bar.z;
Some compilers just do not realise that (&foo.x)[2] is the same data as foo.z and they reorder instructions incorrectly. It is very hard to find bugs like this.
I need to find some way to mock an overload of a function return type in C++.
I know that there isn't a way to do that directly, but I'm hoping there's some out-of-the-box way around it.
We're creating an API for users to work under, and they'll be passing in a data string that retrieves a value based on the string information. Those values are different types. In essence, we would like to let them do:
int = RetrieveValue(dataString1);
double = RetrieveValue(dataString2);
// Obviously, since they don't know the type, they wouldn't use int =.... It would be:
AnotherFunction(RetrieveValue(dataString1)); // param of type int
AnotherFunction(RetrieveValue(dataString2)); // param of type double
But that doesn't work in C++ (obviously).
Right now, we're having it set up so that they call:
int = RetrieveValueInt(dataString1);
double = RetrieveValueDouble(dataString2);
However, we don't want them to need to know what the type of their data string is.
Unfortunately, we're not allowed to use external libraries, so no using Boost.
Are there any ways we can get around this?
Just to clarify, I understand that C++ can't natively do it. But there must be some way to get around it. For example, I thought about doing RetrieveValue(dataString1, GetType(dataString1)). That doesn't really fix anything, because GetType also can only have one return type. But I need something like that.
I understand that this question has been asked before, but in a different sense. I can't use any of the obvious answers. I need something completely out-of-the-box for it to be useful to me, which was not the case with any of the answers in the other question asked.
You've to start with this:
template<typename T>
T RetrieveValue(std::string key)
{
//get value and convert into T and return it
}
To support this function, you've to work a bit more, in order to convert the value into the type T. One easy way to convert value could be this:
template<typename T>
T RetrieveValue(std::string key)
{
//get value
std::string value = get_value(key, etc);
std::stringstream ss(value);
T convertedValue;
if ( ss >> convertedValue ) return convertedValue;
else throw std::runtime_error("conversion failed");
}
Note that you still have to call this function as:
int x = RetrieveValue<int>(key);
You could avoid mentioning int twice, if you could do this instead:
Value RetrieveValue(std::string key)
{
//get value
std::string value = get_value(key, etc);
return { value };
}
where Value is implemented as:
struct Value
{
std::string _value;
template<typename T>
operator T() const //implicitly convert into T
{
std::stringstream ss(_value);
T convertedValue;
if ( ss >> convertedValue ) return convertedValue;
else throw std::runtime_error("conversion failed");
}
}
Then you could write this:
int x = RetrieveValue(key1);
double y = RetrieveValue(key2);
which is which you want, right?
The only sane way to do this is to move the return value to the parameters.
void retrieve_value(std::string s, double& p);
void retrieve_value(std::string s, int& p);
<...>
double x;
retrieve_value(data_string1, x);
int y;
retrieve_value(data_string2, y);
Whether it is an overload or a specialization, you'll need the information to be in the function signature. You could pass the variable in as an unused 2nd argument:
int RetrieveValue(const std::string& s, const int&) {
return atoi(s.c_str());
}
double RetrieveValue(const std::string& s, const double&) {
return atof(s.c_str());
}
int i = RetrieveValue(dataString1, i);
double d = RetrieveValue(dataString2, d);
If you know your value can never be something like zero or negative, just return a struct holding int and double and zero out the one you don't need...
It's a cheap and dirty, but easy way...
struct MyStruct{
int myInt;
double myDouble;
};
MyStruct MyFunction(){
}
If the datastrings are compile-time constants (as said in answering my comment), you could use some template magic to do the job. An even simpler option is to not use strings at all but some data types which allow you then to overload on argument.
struct retrieve_int {} as_int;
struct retrieve_double {} as_double;
int RetrieveValue(retrieve_int) { return 3; }
double RetrieveValue(retrieve_double) { return 7.0; }
auto x = RetrieveValue(as_int); // x is int
auto y = RetrieveValue(as_double); // y is double
Unfortunately there is no way to overload the function return type see this answer
Overloading by return type
int a=itoa(retrieveValue(dataString));
double a=ftoa(retrieveValue(dataString));
both return a string.
As an alternative to the template solution, you can have the function return a reference or a pointer to a class, then create subclasses of that class to contain the different data types that you'd like to return. RetrieveValue would then return a reference to the appropriate subclass.
That would then let the user pass the returned object to other functions without knowing which subclass it belonged to.
The problem in this case would then become one of memory management -- choosing which function allocates the returned object and which function deletes it, and when, in such a way that we avoid memory leaks.
The answer is simple just declare the function returning void* type and in the definition return a reference to the variable of different types. For instance in the header (.h) declare
void* RetrieveValue(string dataString1);
And in the definition (.cpp) just write
void* RetrieveValue(string dataString1)
{
if(dataString1.size()<9)
{
static double value1=(double)dataString1.size();
return &value1;
}
else
{
static string value2=dataString1+"some string";
return &value2;
}
}
Then in the code calling RetrieveValue just cast to the right value
string str;
string str_value;
double dbl_value;
if(is_string)
{
str_value=*static_cast<*string>(RetrieveValue(str));
}
else
{
dbl_value=*static_cast<*double>(RetrieveValue(str));
}
Since you used an example that wasn't really what you wanted, you threw everyone off a bit.
The setup you really have (calling a function with the return value of this function whose return type is unknowable) will not work because function calls are resolved at compile time.
You are then restricted to a runtime solution. I recommend the visitor pattern, and you'll have to change your design substantially to allow for this change. There isn't really another way to do it that I can see.
I have the following C++ code:
Some_class * temp1 = findTemp1(...); // returns NULL or a valid pointer
Some_class * temp2 = findTemp2(...); // returns NULL or a valid pointer
Some_class * temp3 = findTemp3(...); // returns NULL or a valid pointer
Now I would like to count how many of these returned a valid pointer (0, 1, 2 or 3).
The only way I can think of is just to test them one by one:
int count = 0;
if (temp1)
count++;
if (temp2)
count++;
if (temp3)
count++;
For 3 pointers, it's not too bad, but it doesn't scale well. Is there a more efficient way assuming I don't redefine the findTempN funcitons (to maybe pass in the counter)?
Thanks a lot for your quick replies! No, I am not going to change the code, I was just wondering what were my other options. I also realized that I cannot be asking for something "scalable" if I am using distinct literals like that to define the 3 pointers. Of course, I didn't think of the things you replied :)
Well, since this is C++ we can go crazy in the quest for terseness... for example:
int count = !!temp1 + !!temp2 + !!temp3;
Update: I probably owe Ivan an explanation of what's going on here.
Assuming temp is any kind of pointer, !temp forces the coercion of the pointer's value to bool (we want to do this) and negates the result (this is a side effect that we do not want). This results in true if the pointer is null and false if the pointer is not null, which is the opposite of what we 'd like. So we add another ! in front to negate the result again.
This leaves us with adding three bool values which coerces them to int and performs the addition, whereupon we have our final result.
You might find it easier to understand the completely equivalent
int count = (bool)temp1 + (bool)temp2 + (bool)temp3;
which I did not use because typing !! is three characters shorter than (bool) (note: you might think that this is a nice trick, but when writing code it is a really bad idea to make decisions based on how many characters you have to type).
The moral of the story is that doing this type of thing can be called either clever or atrocious, depending on who you ask -- but in C++ there has traditionally been high tolerance for atrocities.
Note that if the pointers were in some type of collection to begin with, you could write much better-looking code using std::count_if, e.g.:
bool isNotNull(void* ptr) {
return ptr != 0;
}
std::vector<Some_class*> vec;
vec.push_back(temp1);
vec.push_back(temp2);
vec.push_back(temp3);
int count = std::count_if(vec.begin(), vec.end(), isNotNull);
See it in action.
Or, as very cleverly suggested by MSalters in the comments, you can lose the isNotNull function by counting the pointers which are 0 and subtracting this from the number of all pointers -- but for this, you will need to somehow know what this number is (easy if they are in a vector):
int count = vec.size() - std::count(vec.begin(), vec.end(), 0);
See it in action.
#define N 3
typedef Some_class *PointerGenerator(...);
PointerGenerator funcs[N];
func[0] = &findTemp1;
func[1] = &findTemp2;
func[2] = &findTemp3;
Some_class *ptrs[N];
for(size_t i = 0; i < N; ++i) ptrs[i] = func[i]();
for(size_t i = 0; i < N; ++i) { if(ptrs[i]) ++count; }
C++0x variant:
int count = std::count_if(ptrs, ptrs + N, [](const Some_class *i) -> bool { return i != NULL; } );
The code you have is Good Enough, don't mess with it.
Introducing subtlety is a common novice error, don't do it.
That said, NULL is a valid pointer value, and you can do e.g. count += !!temp1 + !!temp2 + !!temp3 (but that would be newbie obfuscation, do not actually do that).
Cheers & hth.,
If all of the pointers are the same type, put the pointers in a table,
or if you can't do that, make a table of pointers to the pointers. Then
use std::count or std::count_if. Something like:
SomeClass** pointerTable[] =
{
&temp1,
&temp2,
&temp3,
// ...
};
struct IndirectIsNoNull
{
bool operator()( SomeClass** p ) const
{
return *p != NULL;
}
};
// ...
int validPointerCount = std::count_if( begin( pointerTable ),
end( pointerTable ),
IndirectIsNoNull() );
Introduce a static counter.
template<typename T>
struct ValidPointer
{
static unsigned int count;
};
template<typename T>
unsigned int ValidPointer<T>::count = 0;
template<typename T>
static void isValid (const T* const p)
{
if(p)
ValidPointer<T*>::count++;
}
Usage:
isValid(temp1);
isValid(temp2);
...
At any point of time, if you want to retrieve then,
unsigned int count = ValidPointer<Some_class*>::count;
This code can be improved as per your requirement.
Do you need the pointers afterwards? temp suggests otherwise. In that case, you can eliminate those:
int count = 0;
if (findTemp1())
count++;
if (findTemp2())
count++;
if (findTemp3())
count++;
Hide the counter in a class:
class Some_class {};
typedef Some_class* (*FindFunction_t)();
Some_class* findTemp1() {return NULL;}
Some_class* findTemp2() {return new Some_class;}
Some_class* findTemp3() {return new Some_class;}
class Finder
{
public:
Finder() : count_(0) {}
Some_class* CallAndCount(FindFunction_t fn) {return Count(fn());}
int GetCount() const {return count_;}
private:
Some_class* Count(Some_class* p) {if(p) count_++; return p;}
int count_;
};
int main()
{
Finder f;
Some_class* temp1 = f.CallAndCount(findTemp1);
Some_class* temp2 = f.CallAndCount(findTemp2);
Some_class* temp3 = f.CallAndCount(findTemp3);
std::wcout << f.GetCount() << L"\n";
}
The names aren't the best and there are memory leaks but you should get the idea.
I think this meets your objective of scalability although you would need to add template functions if your find functions were to take parameters