473,394 Members | 1,749 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,394 software developers and data experts.

Compile-time range check?

I have a template:

template<typename c, unsigned size>
struct Vector {
c components[size];
template<unsigned index>
c &get() { return components[index]; }
};

Vector<double, 3vect;
Is there a way to cause vect.get<3>() to fail at compile time. but
vect.get<2>() have compile successfully?
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Jun 27 '08 #1
4 3101
Daniel Pitts wrote:
I have a template:

template<typename c, unsigned size>
struct Vector {
c components[size];
template<unsigned index>
c &get() { return components[index]; }
};

Vector<double, 3vect;
Is there a way to cause vect.get<3>() to fail at compile time. but
vect.get<2>() have compile successfully?
Sure. Read up on compile-time asserts.

Here is what I could come up with (in five minutes or so it took to type
it up):

template<unsigned low, unsigned test, unsigned high>
struct is_seq {
static char _[low <= test && test <= high];
static const bool yes = sizeof(_);
};
...
template<unsigned indexc &get() {
return is_seq<0,index,size-1>::yes, components[index];
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
Victor Bazarov wrote:
Daniel Pitts wrote:
>I have a template:

template<typename c, unsigned size>
struct Vector {
c components[size];
template<unsigned index>
c &get() { return components[index]; }
};

Vector<double, 3vect;
Is there a way to cause vect.get<3>() to fail at compile time. but
vect.get<2>() have compile successfully?

Sure. Read up on compile-time asserts.

Here is what I could come up with (in five minutes or so it took to type
it up):

template<unsigned low, unsigned test, unsigned high>
struct is_seq {
static char _[low <= test && test <= high];
static const bool yes = sizeof(_);
};
...
template<unsigned indexc &get() {
return is_seq<0,index,size-1>::yes, components[index];
}

V
Thanks.
I can be more specific than that, because all I really care about is
0..max. I managed to come up with:

template<bool>
struct InvalidIndex{};

template<>
struct InvalidIndex<false>{ enum { isValid }; };

template<unsigned index, unsigned max>
struct ValidIndex {
enum { isValid = InvalidIndex<index >= max>::isValid };
};

Thanks for the reply.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Jun 27 '08 #3
Daniel Pitts wrote:
I have a template:

template<typename c, unsigned size>
struct Vector {
c components[size];
template<unsigned index>
c &get() { return components[index]; }
};

Vector<double, 3vect;
Is there a way to cause vect.get<3>() to fail at compile time. but
vect.get<2>() have compile successfully?

please check BOOST_STATIC_ASSERT.
Jun 27 '08 #4
Daniel Pitts wrote:
I have a template:

template<typename c, unsigned size>
struct Vector {
c components[size];
template<unsigned index>
c &get() { return components[index]; }
};

Vector<double, 3vect;
Is there a way to cause vect.get<3>() to fail at compile time. but
vect.get<2>() have compile successfully?

Another alternative is to use boost::enable_if and mpl helpers.

#include <iostream>

#include <boost/mpl/and.hpp>
#include <boost/mpl/int.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/mpl/greater.hpp>
#include <boost/mpl/less.hpp>

namespace mpl = boost::mpl;

template < typename C, unsigned SIZE >
struct Vector
{
C components[SIZE];

template < unsigned I >
typename boost::enable_if
<
mpl::and_
<
mpl::greater< mpl::int_<I>, mpl::int_<-1
, mpl::less< mpl::int_<I>, mpl::int_<SIZE
>
, C &
>::type get()
{
return components[i];
}
};

int main()
{
Vector<double, 3v = { 42.3, 66.6, 0.1 };

std::cout << v.get<1>() << std::endl;
//std::cout << v.get<3>() << std::endl;

std::cin.get();
}

Of course, you'll probably wish to clean that up a bit by writing your
own range checking metafunction.
Jun 27 '08 #5

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

Similar topics

8
by: janeaustine50 | last post by:
Python's InteractiveInterpreter uses the built-in compile function. According to the ref. manual, it doesn't seem to concern about the encoding of the source string. When I hand in an unicode...
1
by: Mario T. Lanza | last post by:
I am working with Visual Studio. The solution I am developing is composed of about 8 separate projects. Some of these projects represent different tiers in the N-tiered architecture (data,...
4
by: frankg | last post by:
When I compile this file ------------------------------------------------------ #include <stdarg.h> template <class Any> void var_arg_func(int dimension_count, ...) { int dimensions;...
0
by: Jordan Willms | last post by:
My xsl stylesheet is as simple as follows: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet xmlns:ims="http://www.imsglobal.org/xsd/imsmd_v1p2"...
6
by: Thomas Connolly | last post by:
I have 2 pages referencing the same codebehind file in my project. Originally the pages referenced separate code behind files. Once I changed the reference to the same file, everything worked...
4
by: tony | last post by:
Hello! My question is about calling this method CollectData below but I get a compile error that I shouldn't have because the type parameter is correct. The compile error is the following:...
1
by: brianrpsgt1 | last post by:
Newbie here.... I have been able to successful pull info from a MySQL DB, get the results and output them in an HTML format using Cheetah to the screen using IDLE. I am doing this on a Windows...
3
by: NvrBst | last post by:
Right now I have C99 code in .c extensions. I compile it in VSC++ and it complains about a lot of errors. I change the extensions to .cpp and compile in VSC++ and it succeeds. Is there a way...
2
by: Andrus | last post by:
I need compile in-memory assembly which references to other in-memory assembly. Compiling second assembly fails with error Line: 0 - Metadata file 'eed7li9m, Version=0.0.0.0, Culture=neutral,...
6
by: Ed Leafe | last post by:
I've noticed an odd behavior with compile() and code that does not contain a trailing newline: if the last line is a comment inside of any block, a syntax error is thrown, but if the last line is a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.