473,320 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

const or not const, that's the question here! :-)

The compiler which I have to use in a project doesn't like
the following construct. It says: 'bitset' : invalid template argument.
It means that it wants only a static-like const N as a template argument,
ie. it doesn't accept a const variable that was initialized
from an auto variable (here i), for passing it as a template
argument to bitset<n>.

Is this a compiler limitation or is my code not standard conform?
Can you think of any workaround construct to achieve this
or an alternate functionality with such a compiler?

Of course if I change the line to read for example
const size_t N = 1U << 16;
then it compiles ok. But I need to loop 22 times here...

#include <bitset>
void f()
{
for (size_t i = 1; i <= 22; ++i)
{
const size_t N = 1U << i;
bitset<Nbv;

bv.set(7);
//...
}
}

Jun 27 '08 #1
8 1197
Adem24 wrote:
The compiler which I have to use in a project doesn't like
the following construct. It says: 'bitset' : invalid template argument.
It means that it wants only a static-like const N as a template argument,
ie. it doesn't accept a const variable that was initialized
from an auto variable (here i), for passing it as a template
argument to bitset<n>.

Is this a compiler limitation or is my code not standard conform?
Can you think of any workaround construct to achieve this
or an alternate functionality with such a compiler?

Of course if I change the line to read for example
const size_t N = 1U << 16;
then it compiles ok. But I need to loop 22 times here...

#include <bitset>
void f()
{
for (size_t i = 1; i <= 22; ++i)
{
const size_t N = 1U << i;
bitset<Nbv;
Your code is wrong. This N is not a compile time constant. Templates only
work with those.

You might want to have a look into dynamic_bitset from boost or
vector<bool>.

bv.set(7);
//...
}
}

Best

Kai-Uwe Bux
Jun 27 '08 #2

"Adem24" <ad****@nospammplease.org.invalida écrit dans le message de news:
g2**********@aioe.org...
The compiler which I have to use in a project doesn't like
the following construct. It says: 'bitset' : invalid template argument.
It means that it wants only a static-like const N as a template argument,
ie. it doesn't accept a const variable that was initialized
from an auto variable (here i), for passing it as a template
argument to bitset<n>.

Is this a compiler limitation or is my code not standard conform?
Can you think of any workaround construct to achieve this
or an alternate functionality with such a compiler?

Of course if I change the line to read for example
const size_t N = 1U << 16;
then it compiles ok. But I need to loop 22 times here...

#include <bitset>
void f()
{
for (size_t i = 1; i <= 22; ++i)
{
const size_t N = 1U << i;
bitset<Nbv;

bv.set(7);
//...
}
}
Your code cannot compile. N must be a compile time evaluatable constant. In
your case, N is constant but unknow to the compiler at compile time because
of i.

----------------
Eric Pruneau
Jun 27 '08 #3

"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:g2**********@aioe.org...
Adem24 wrote:
>The compiler which I have to use in a project doesn't like
the following construct. It says: 'bitset' : invalid template argument.
It means that it wants only a static-like const N as a template argument,
ie. it doesn't accept a const variable that was initialized
from an auto variable (here i), for passing it as a template
argument to bitset<n>.

Is this a compiler limitation or is my code not standard conform?
Can you think of any workaround construct to achieve this
or an alternate functionality with such a compiler?

Of course if I change the line to read for example
const size_t N = 1U << 16;
then it compiles ok. But I need to loop 22 times here...

#include <bitset>
void f()
{
for (size_t i = 1; i <= 22; ++i)
{
const size_t N = 1U << i;
bitset<Nbv;

Your code is wrong. This N is not a compile time constant. Templates only
work with those.
You could probably set this up fairly simply to use compile time recursion.
Use a functor templated on N which calls N-1 and specialize it for zero (or
other way up ) case etc...

regards
Andy Little
Jun 27 '08 #4
kwikius wrote:
>
"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:g2**********@aioe.org...
>Adem24 wrote:
>>The compiler which I have to use in a project doesn't like
the following construct. It says: 'bitset' : invalid template argument.
It means that it wants only a static-like const N as a template
argument, ie. it doesn't accept a const variable that was initialized
from an auto variable (here i), for passing it as a template
argument to bitset<n>.

Is this a compiler limitation or is my code not standard conform?
Can you think of any workaround construct to achieve this
or an alternate functionality with such a compiler?

Of course if I change the line to read for example
const size_t N = 1U << 16;
then it compiles ok. But I need to loop 22 times here...

#include <bitset>
void f()
{
for (size_t i = 1; i <= 22; ++i)
{
const size_t N = 1U << i;
bitset<Nbv;

Your code is wrong. This N is not a compile time constant. Templates only
work with those.

You could probably set this up fairly simply to use compile time
recursion. Use a functor templated on N which calls N-1 and specialize it
for zero (or other way up ) case etc...
Yes one could do that. But why? You end up with some monstrous incantation
that will fill your screen with book size error messages if you get a tiny
detail wrong; and for which gain? The algorithms underlying bitset<Nare
mostly just bit fiddling iterated over a vector of unsigned long (int?)
anyway. I would be rather surprised if it matters performancewise whether
the length of that buffer is fixed at compile time or at run time.

I love templates, I really do; but in a case like this, I think they would
make the code worse (e.g., harder to maintain and change).
Best

Kai-Uwe Bux
Jun 27 '08 #5
On Jun 13, 10:42*pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
kwikius wrote:
"Kai-Uwe Bux" <jkherci...@gmx.netwrote in message
news:g2**********@aioe.org...
Adem24 wrote:
>The compiler which I have to use in a project doesn't like
the following construct. It says: 'bitset' : invalid template argument..
It means that it wants only a static-like const N as a template
argument, ie. it doesn't accept a const variable that was initialized
from an auto variable (here i), for passing it as a template
argument to bitset<n>.
>Is this a compiler limitation or is my code not standard conform?
Can you think of any workaround construct to achieve this
or an alternate functionality with such a compiler?
>Of course if I change the line to read for example
* *const size_t N = 1U << 16;
then it compiles ok. But I need to loop 22 times here...
>#include <bitset>
void f()
{
* * for (size_t i = 1; i <= 22; ++i)
* * * {
* * * * const size_t N = 1U << i;
* * * * bitset<Nbv;
Your code is wrong. This N is not a compile time constant. Templates only
work with those.
You could probably set this up fairly simply to use compile time
recursion. Use a functor templated on N which calls N-1 and specialize it
for zero (or other way up ) case etc...

Yes one could do that. But why?
Only one way to find out....

<... usual inertia elided ...>

regards
Andy Little

Jun 27 '08 #6
kwikius wrote:
On Jun 13, 10:42 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>kwikius wrote:
>>You could probably set this up fairly simply to use compile time
recursion. Use a functor templated on N which calls N-1 and specialize it
for zero (or other way up ) case etc...
Yes one could do that. But why?

Only one way to find out....

<... usual inertia elided ...>
Apologies That was a bit fierce, however using compile time loop
unrolling is a useful technique, and the OP was asking why it wasnt
didnt seem possible. I was merely pointing out that it is possible. In
some cases the compiler may do loop unrolling for you, but doing it
explicitly using compile time recursion is one less uncertainty/ layer
of complexity for the optimiser.

Interestingly, looking at std::bitset, Its kind of a weird hybrid
because the length is fixed but many ops are runtime.

As an example one could add a bitset.flip<Pos>() function to accompany
bitset.flip(pos). The first version being again easier to optimise and
all the information re the op is available at compile time unlike the
runtime parameter, and very usefully, the range can be checked at
compile time too, so there is no need of a runtime check and possible
exception, unlike appears to be the case in the runtime flip (which
throws an exception on out of range). The same applies to many (probably
nearly all) ops in bitset AFAICS.

regards
Andy Lttle
Jun 27 '08 #7
Hi!

kwikius schrieb:
You could probably set this up fairly simply to use compile time recursion.
Use a functor templated on N which calls N-1 and specialize it for zero (or
other way up ) case etc...
You could probably fix this fairly simple by using the same size in each
loop:

const size_t maxlen = 22;
bitset<1 << maxlenbv;
for (size_t i = 1; i <= maxlen; ++i)
{
const size_t N = 1U << i;

bv.set(7);
//...
}

Frank
Jun 27 '08 #8
Andy Little wrote:
kwikius wrote:
>On Jun 13, 10:42 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>>kwikius wrote:
>>>You could probably set this up fairly simply to use compile time
recursion. Use a functor templated on N which calls N-1 and
specialize it for zero (or other way up ) case etc...
Yes one could do that. But why?

Only one way to find out....

<... usual inertia elided ...>

Apologies That was a bit fierce, however using compile time loop
unrolling is a useful technique, and the OP was asking why it wasnt
didnt seem possible. I was merely pointing out that it is possible.
In some cases the compiler may do loop unrolling for you, but doing
it explicitly using compile time recursion is one less uncertainty/
layer of complexity for the optimiser.

Interestingly, looking at std::bitset, Its kind of a weird hybrid
because the length is fixed but many ops are runtime.

As an example one could add a bitset.flip<Pos>() function to
accompany bitset.flip(pos). The first version being again easier
to optimise and all the information re the op is available at
compile time unlike the runtime parameter, and very usefully, the
range can be checked at compile time too, so there is no need of a
runtime check and possible exception, unlike appears to be the case
in the runtime flip (which throws an exception on out of range).
The same applies to many (probably nearly all) ops in bitset AFAICS.
Any decent optimizer can do that anyway, when pos is a compile time
constant. That saves you from modifying the application code when that
condition changes.
Bo Persson
Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: cheeser | last post by:
Hello all, Please see the question in the code below... Thanks! Dave #include <iostream>
12
by: Christof Krueger | last post by:
Hello, I'm quite new to C++ so maybe there's something I miss. I write a simple board game. It has a board class. This class has a method that returns the count of pieces a player has on the...
2
by: Pavel | last post by:
I am writing software for an embedded application and here is the question. GCC would emit data declared like const char text = "abc"; to .rodata (i.e. "read only data") section. I can put this...
9
by: vashwath | last post by:
Hi all, Recently I attended an interview in which the question "Is there any difference between "const T var" and "T const var"? was asked.I answered "NO"(I guessed it:-( ).Did I answered...
20
by: Snis Pilbor | last post by:
Whats the point of making functions which take arguments of a form like "const char *x"? It appears that this has no effect on the function actually working and doing its job, ie, if the function...
42
by: S S | last post by:
Hi Everyone I have const char *p = "Hello"; So, here memory is not allocated by C++ compiler for p and hence I cannot access p to modify the contents to "Kello" p = 'K'; // error at runtime
14
by: Tim H | last post by:
I understand the semantics of why this works the way it does. But I wonder if there's a reason for the behaviore at the line marked "QUESTION". I figured if there is an answer, someone here knows...
14
by: Jonas.Holmsten | last post by:
Hello I'm porting some C++ stuff to C and having problem to get it through gcc. Here is a condensed version of the problem: void foo(const int * const * const ptr) {} main()
29
by: Rohit kumar Chandel | last post by:
Hi all, I have a doubt in const keyword. My understanding says that a variable declared const can not be modified by the module it is defined in. Then consider the following code segment:...
10
by: Stephen Howe | last post by:
Hi Just going over some grey areas in my knowledge in C++: 1) If I have const int SomeConst = 1; in a header file, it is global, and it is included in multiple translations units, but it...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.