You are here:

C++/How to make a function return a constant int

Advertisement


Question
Hello,

I'd like to construct a set of bitsets (using C++ STL), where the length iof the bitsets is determined by the output of a function (but all bitsets in the set will have the same length). I am getting errors that the int the function returns is not constant, and therefore cannot be used in the bitset template. I've tried typecasting but it hasn't worked. Here's an example of what I'd like to do, but cannot:

#include <bitset>
#include <set>

int some_function(int n);

int main() {
 const int size = (const int) some_function(3);
 set< bitset<size>> my_bitset_set;
}

int some_function(int n) {
 return n;
}

Any help would be greatly appreciated!

Answer
You cannot use std::bitset in this way, just as you could not use size for the dimension of a non-dynamic built in array, and for the same reason:

 const int size = (const int) some_function(3);

 int int_array[size]; // Not allowed, arrays sized at compile time

Template parameters need to be specified at _compile_ time just as (non dynamic) array dimensions need to be known at compile time. As size is returned from a function executed at run time the compiler cannot calculate its value and therefore cannot plug the value of size into the std::bitset class template to create a specific bitset class.

That's right, by specifying template parameters you cause the compiler to create a new class type (or a new function in the case of a function template). That is why they are called templates - they are templates that can be used by the compiler to create classes (or functions). As these are used by the compiler all information to convert a template into an actual class (or function) has to be available to the compiler at the point it uses a template to create a class (or function).

The best alternative I can offer from the C++ standard library is to try using a std::vector<bool>, which does not have all the nice bit operations of std::bitset, but most implementations do use one bit per bool and use a proxy class to help in the access of elements. This allows for bit-level operations on what would otherwise be a bool type, most noticeably the flip() operation is available for single (bit) elements as well as the whole of a std::vector<bool>.

The only other alternative I can think of is to use a std::bitset large enough for any reasonable value of size.

If you can use libraries other than the C++ standard library then I suggest you start by looking at the Boost libraries (http://www.boost.org/), specifically the Boost Dynamic Bitset library (http://www.boost.org/libs/dynamic_bitset/dynamic_bitset.html), which does exactly what you want as the number of bits is defined at construction time (i.e. at run time) and not statically via a template parameter at compile time.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2012 About.com, a part of The New York Times Company. All rights reserved.