I have a template:
template<typena me c, unsigned size>
struct Vector {
c components[size];
template<unsign ed 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/> 4 3129
Daniel Pitts wrote:
I have a template:
template<typena me c, unsigned size>
struct Vector {
c components[size];
template<unsign ed 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<unsign ed low, unsigned test, unsigned high>
struct is_seq {
static char _[low <= test && test <= high];
static const bool yes = sizeof(_);
};
...
template<unsign ed 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
Victor Bazarov wrote:
Daniel Pitts wrote:
>I have a template:
template<typen ame c, unsigned size> struct Vector { c components[size]; template<unsign ed index> c &get() { return components[index]; } };
Vector<doubl e, 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<unsign ed low, unsigned test, unsigned high>
struct is_seq {
static char _[low <= test && test <= high];
static const bool yes = sizeof(_);
};
...
template<unsign ed 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<fa lse>{ enum { isValid }; };
template<unsign ed index, unsigned max>
struct ValidIndex {
enum { isValid = InvalidIndex<in dex >= max>::isValid };
};
Thanks for the reply.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity .net/wordpress/>
Daniel Pitts wrote:
I have a template:
template<typena me c, unsigned size>
struct Vector {
c components[size];
template<unsign ed 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_AS SERT.
Daniel Pitts wrote:
I have a template:
template<typena me c, unsigned size>
struct Vector {
c components[size];
template<unsign ed 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_i f 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_i f
<
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 object, it is encoded in utf-8 automatically.
It can be a problem when I'm building an interactive environment using
"compile", with a different encoding from utf-8. IDLE itself has the
same problem. ( '<a string with non-ascii-encoding>' is...
|
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, business
logic, presentation, etc.). Right now, some of the projects are
inter-related and reference each other using Project References. When
I select "Rebuild Solution" to compile, each project is successfully
compiled into its own directory (and...
|
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;
va_list ap;
|
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"
xmlns="http://ltsc.ieee.org/xsd/LOMv1p0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" encoding="ISO-8859-1"
indent="yes"/>
<!-- rename ims datetime tags to IEEE dateTime tags -->
<xsl:template match="ims:datetime">
|
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 fine while the file was in the
project directory. When the obsolete file was removed
from the project directory, my application will no longer
compile. Can someone please help with this issue?
Thank in advance,
Tom
| |
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:
C:\PK\Development\Products\UTCAS\4.0\SRC\MeltPracApplication\Dialog\Composit
ionForm.cs(942): Argument '1': cannot convert from 'ref
MeltPracData.MeltPracDataComposition' to 'ref
MeltPracCommon.IDialogPostData'
|
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 Laptop, running WinXP,
Python 2.5 and the latest version of Cheetah.
I have two questions:
1. How and where do you compile Cheetah templates in Windows? The
command in the docs is cheetah compile a, however, I believe that this
|
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 to keep my extensions .c, but tell VSC++ (through an
project option) to compile as C++98 code (as if it had .cpp
extensions).
|
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,
PublicKeyToken=null' could not be found
Saving assembly to Windows temp directory for referencing only creates huge
amout of temporary
|
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 non-
comment Python statement, there is no error. Here's an example (using
2.5.1 on OS X)
.... def foo():
.... print 'bar' """
<code object <moduleat 0x79608, file "", line 2>
|
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...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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,...
|
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...
|
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...
| |
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |