Bitset template error

  • Thread starter Michal Sekletar
  • Start date
M

Michal Sekletar

Hello,

I am programming NetFlow collector and in module dealing with sorting ip
addresses into classes according do predefined rules I want to create
std::bitset to represent network mask. I have a method
void foo(struct in_addr addr, const size_t len).

I want to create bit set in this method std::bitset<len> b; but compiler
is complaining :
trie.cpp:22:17: error: ‘len’ cannot appear in a constant-expression
trie.cpp:22:20: error: template argument 1 is invalid.

It is fine to create bitset like this std::bitset<24> b; on the other
hand if I pass net mask length as an argument compilation ends up with
an error.
So, am I forgetting something important about std::bitset template class?

Thanks
 
J

Jeff Flinn

Michal said:
Hello,

I am programming NetFlow collector and in module dealing with sorting ip
addresses into classes according do predefined rules I want to create
std::bitset to represent network mask. I have a method
void foo(struct in_addr addr, const size_t len).

I want to create bit set in this method std::bitset<len> b; but compiler
is complaining :
trie.cpp:22:17: error: ‘len’ cannot appear in a constant-expression
trie.cpp:22:20: error: template argument 1 is invalid.

It is fine to create bitset like this std::bitset<24> b; on the other
hand if I pass net mask length as an argument compilation ends up with
an error.
So, am I forgetting something important about std::bitset template class?

Thanks

As Victor stated, std::bitset cannot be used in this fashion, see boost
dynamic_bitset for an alternative.

Jeff
 
M

Michal Sekletar

No, you're forgetting something important about constant-expressions. A
function argument is not a constant-expression, regardless of the
qualifications (in your case you qualified the argument 'const'). Since
the argument is initialized at run-time (when the function is actually
called), its value is not known until run-time, and the compiler cannot
use that argument to instantiate a template with a non-type template
argument of the type 'size_t' (like in the case of 'std::bitset').

V

Thank you for your advice.

Michal Sekletar
 
J

Juha Nieminen

Jeff Flinn said:
As Victor stated, std::bitset cannot be used in this fashion, see boost
dynamic_bitset for an alternative.

Or std::vector<bool> if you want to keep within the current standard.
 
J

Juha Nieminen

Michal Sekletar said:
It is fine to create bitset like this std::bitset<24> b; on the other
hand if I pass net mask length as an argument compilation ends up with
an error.
So, am I forgetting something important about std::bitset template class?

The size of std::bitset has to be determined at compile time (like a
static array). It cannot be determined at runtime.

Use std::vector<bool> for a bit vector which size is determined at
runtime (yes, each value will take only 1 bit of memory).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,217
Latest member
topweb3twitterchannels

Latest Threads

Top