Would this be a propper useage of macros?

I would like it that bitsets would throw an error if they are assigned a value outside of their bitrange. Currently the standard implementation just uses modular arrithmatic to assign the value of 0001 to bitset<4> a(16);

could I do something like

1
2
3
4
5
6
#define old_assignment_operator(size, name, param) bitset<size> #name (param)
#define bitset<size> /*ok I really don't know how to do this, but I want to hide 
the standard declarator for bitset<any_size>.  How would I do that? */ /
/*check for compliance, if failure: cerr << "Sorry out of range error.\n" << endl;*/ /
old_assignment_operator(/*yada yada yada*/)
//end of macro. 
I'm not sure I follow what you're trying to do, but...

You can't use macros to overload / change behavior of the assignment operator. The only thing macros do is substitute a block of code into an identifier. However that identifier can't have symbols (like an operator).

I want to hide the standard declarator for bitset<any_size>


It's a public interface. The only way to hide it would be to not #include the interface.
What I mean by hiding is thus:

1
2
3
4
5
6
7
8
#include <bitset>
using namespace std;

int main()
{
    bitset<4> a;
    return 0;
}


compiles.


1
2
3
4
5
6
7
8
9
#include <bitset>
using namespace std;

#define bitset

int main()
{
    bitset<4> a;
}


does not compile.

I would like to have a macro that takes as a paramter an unsigned int, but instead of needing to enclose the parameter within () to have them enclosed within <>.

That way I would be able to with a single couple lines of code replace all instances of bitsets into an implementation that is more robust, say, by giving an error message if you were to try to force the number 16 into a bitset<4>, which only has enough bits to represent the numbers 0 - 15. etc etc etc

Is that more clear now?
for example:
1
2
3
4
5
6
7
8
#define example(uint, name, value) bitset<uint> name(value)

int main()
{
    //bitset<4> B1(1); //original line of code
    example(4, B1, 1); //new line of code
    cout << "B1 = " << B1 << ".\n" << endl;
}


The above compiles and works fine, but I would like to be able to call the macro with the exact same syntax as the original declarator like:
1
2
3
4
int main()
{
    example<4> B1(1);
}

only replace example with bitset.

This would allow for very easy find & replace of very large code.
bump
The preprocessor can do this in the same way that the compiler can let you make a function call by passing actual parameters using template syntax (a_function<a_run_time_value>;). Which is to say, not at all.

Sounds like what you want is to wrap std::bitset with your own templated class. Then you can make whatever checks you want before calling the problematic functions.
 
The preprocessor can do this in the same way that the compiler can let you make a function call by passing actual parameters using template syntax (a_function<a_run_time_value>;). Which is to say, not at all.


Come again?
Topic archived. No new replies allowed.