473,750 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

compile time value -> index calculation

Hi all,

in the following example, Index<unsigned int x>::value allows to calculate the "rounded up" index
from any "increasing " value during compile time. Unfortunately, the definition of the index - value
pairs does not really look nice.

The definition through an array would look much nicer, but does not work, see below.

As the definition of the index - value pairs are supposed to become part of the API, does anyone
have an idea how this could be beautified?

Thanks,

Christof

#include <iostream>
using namespace std;

// definition of the index - value pairs
template<unsign ed int indexstruct Value {};
template<struct Value<0{static const unsigned int value = 1;};
template<struct Value<1{static const unsigned int value = 10;};
template<struct Value<2{static const unsigned int value = 100;};
template<struct Value<3{static const unsigned int value = 1000;};
template<struct Value<4{static const unsigned int value = 10000;};

// much more readable than above, but does not work, see below
const unsigned int a[] = {1, 10, 100, 1000, 10000};

template<unsign ed int size, unsigned int index = 0struct Index {
static const unsigned int value = (size <= Value<index>::v alue ? index : Index<size, index +
1>::value);
// the following does not work, as even const arrays are not allowed to be used in const
expressions?!
//static const unsigned int value = (size <= a[index] ? index : Index<size, index + 1>::value);
};
// needed to terminate recursion, would be nice if it could be calculate from the index - value
definitions from above
template<unsign ed int sizestruct Index<size, 5{
static const unsigned int value = 0xffffffff;
};

int main(void) {
cout << Value<0>::value << " " << Value< 3>::value << " " << Value< 4>::value << endl;
cout << Index<0>::value << " " << Index<100>::val ue << " " << Index<6000>::va lue << endl;
//cout << Value<8>::value << endl; // gives a compiler error as intended
cout << hex << Index<10001>::v alue << endl; // would be nice if this gives a compiler error too
}
Jun 27 '08 #1
3 2396
On May 9, 4:29 pm, Christof Warlich <cwarl...@alcat el-lucent.de>
wrote:
Hi all,

As the definition of the index - value pairs are supposed to become part of the API, does anyone
have an idea how this could be beautified?

Thanks,

Christof

#include <iostream>
using namespace std;

// definition of the index - value pairs
template<unsign ed int indexstruct Value {};
template<struct Value<0{static const unsigned int value = 1;};
template<struct Value<1{static const unsigned int value = 10;};
template<struct Value<2{static const unsigned int value = 100;};
template<struct Value<3{static const unsigned int value = 1000;};
template<struct Value<4{static const unsigned int value = 10000;};
use recursive templates.

enum { multiplier = 10 };

template <unsigned int indexstruct value
{
static const long long int this_value = value< index-1>::this_value
* multiplier;
}

template <struct value<0{ enum { this_value = 1 }; } //
termination condition.

since the depth of template recursion is limited (say 17 is the upper
limit)
template <struct value<17{}; // let there be compilation error.

Jun 27 '08 #2
gnuyuva schrieb:
use recursive templates.

enum { multiplier = 10 };

template <unsigned int indexstruct value
{
static const long long int this_value = value< index-1>::this_value
* multiplier;
}

template <struct value<0{ enum { this_value = 1 }; } //
termination condition.

since the depth of template recursion is limited (say 17 is the upper
limit)
template <struct value<17{}; // let there be compilation error.
Thanks, but this is not what I was looking for.

I should have been more precise: I want to be able to define any arbitrary
indexed sequence of constant values, as long as the values are increasing.
Thus, this could be another example:

// definition of the index - value pairs
template<unsign ed int indexstruct Value {};
template<struct Value<0{static const unsigned int value = 27;};
template<struct Value<1{static const unsigned int value = 33;};
template<struct Value<2{static const unsigned int value = 815;};
template<struct Value<3{static const unsigned int value = 4711;};

Again, writing this with an array would be quite convenient:

const unsigned int a[] = {27, 33, 815, 4711};

but the array elements are obviously not allowed to be used in a constant
expression.

Sorry for making this not clear enough.
Jun 27 '08 #3
On May 9, 6:06 pm, Christof Warlich <cwarl...@alcat el-lucent.de>
wrote:
gnuyuva schrieb:
use recursive templates.
enum { multiplier = 10 };
template <unsigned int indexstruct value
{
static const long long int this_value = value< index-1>::this_value
* multiplier;
}
template <struct value<0{ enum { this_value = 1 }; } //
termination condition.
since the depth of template recursion is limited (say 17 is the upper
limit)
template <struct value<17{}; // let there be compilation error.

Thanks, but this is not what I was looking for.

I should have been more precise: I want to be able to define any arbitrary
indexed sequence of constant values, as long as the values are increasing.
Thus, this could be another example:

// definition of the index - value pairs
template<unsign ed int indexstruct Value {};
template<struct Value<0{static const unsigned int value = 27;};
template<struct Value<1{static const unsigned int value = 33;};
template<struct Value<2{static const unsigned int value = 815;};
template<struct Value<3{static const unsigned int value = 4711;};

Again, writing this with an array would be quite convenient:

const unsigned int a[] = {27, 33, 815, 4711};

but the array elements are obviously not allowed to be used in a constant
expression.

Sorry for making this not clear enough.
Well, in that case you don't have any other go but to write them
manually. Maybe you can reduce your typing effort by writing a macro.
or wait till c++0x for 'constexpr' ;-)
Jun 27 '08 #4

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

Similar topics

17
3129
by: newbiecpp | last post by:
I have hard time to understand run-time environment. Let assume that I have a program that has a simple variable alpha. When this variable is statically allocated, the compiler can use the absolute address of alpha to access to it. What confuses me is that when the variable is dynamically allocated, how does the compiler implement it? We know the address of the variable until run-time. During the compilation, how can we access to the...
5
3817
by: Brice Prunier | last post by:
Here under 4 schemas i'm working with ( it may be long: sorry...) The context is the following : Resident.xsd imports Person.xsd and includes Common.xsd ( anonimous schema: no TargetNamespace ) Person.xsd includes Common-Naming.xsd ( anonimous schemas ) Common-Naming.xsd includes common.xsd ( both are anonimous schemas ) Compilation of Resident.xsd raise the following exception: "System.Xml.Schema.XmlSchemaException: The attribute 'oid'...
2
4644
by: Glen | last post by:
I'm working on a custom assembly and I'm trying to figure out the best approach to handling known constraints within the assembly, once compiled, to alert the developer at compile time of a potential issue. For example, in the assembly I would like to add a constraint that states a particular property member of the class can not be equal to one other property. In standard coding I can throw an exception during run-time, but I would rather...
13
3070
by: Adam Blair | last post by:
Is it possible to bind a switch statement to an Enum such that a compile-time error is raised if not all values within the Enum are handled in the switch statement? I realise you can use default: to catch unhandled cases, but of course this is only at run-time. Example: public enum MyEnum { one, two, three, four }
6
1986
by: Terry Olsen | last post by:
I would like to put program info in my applications' about screen such as compile date & time, lines of code, etc. Is there a way to do this automatically when I compile?
1
10427
by: Frederick Gotham | last post by:
I've mentioned his name a few times here lately, but over on comp.lang.c, Hallvard B Furuseth has come up with a Log-base-2 function that serves as a compile-time constant. I'm not going to pretend to even begin to understand how it works, but if anyone's interested, here's a link to the post: http://groups.google.ie/group/comp.lang.c/msg/706324f25e4a60b0?hl=en&
15
4848
by: steve yee | last post by:
i want to detect if the compile is 32 bits or 64 bits in the source code itself. so different code are compiled respectively. how to do this?
9
3518
by: ThunderMusic | last post by:
Hi, I'd like to create a compile time error in my class... maybe there's a way already built in in the framework so I can achieve what I want... I have 2 constructors in my class. One of them has mandatory parameters, I mean, they should not be null nor empty (for strings). So I'd make the validation in the constructor and generate a compile-time error if the validation does not match... Is there a way to achieve this or to specify...
19
8752
by: Rahul | last post by:
Hi, Is there a way to find the offset of a class member at compile time. e.g. class A{ int i; int j; char c; }; Here the offset of c = 8 bytes from the start of an object of A (assuming 4 byte int). Can it be done at compile time. Thanks in advance
22
3610
by: Tomás Ó hÉilidhe | last post by:
I've been developing a C89 microcontroller application for a while now and I've been testing its compilation using gcc. I've gotten zero errors and zero warnings with gcc, but now that I've moved over to the micrcontroller compiler I'm getting all sorts of errors. One thing I'd like to clarify is the need (in C89) for a compile- time constant in the initialiser of a variable. The compiler rejects the following source file: /* Start...
0
9000
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9577
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9339
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6804
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2804
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2225
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.