C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: Single character of a constant string as template parame

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
kanze@gabi-soft.fr
Guest





PostPosted: Thu Jun 26, 2003 10:31 pm    Post subject: Re: Single character of a constant string as template parame Reply with quote



"Yuriy Sorokin" <yura (AT) cad (DOT) ntu-kpi.kiev.ua> wrote

Quote:
Look at the fragment below

const char ST[]="12345";

template<char CH> struct Digit
{
enum { result=CH-'0' };
};

int main(int argc, char* argv[])
{
Digit<'0'> d1;// Looks good
Digit<ST[0]> d2; //Error
return 0;
}

Is such code legal?

No.

Quote:
ST[0] is a constant expression,

Not according to the standard. There are two types of constant
expressions, integral constant expressions, and other constant
expressions. An integral constant expression "can involve only [...]
const variables or static data members of integral or enumeration types
initialized with constant expressions. [...] In particular, except in
sizeof expressions, funcitons, class objects, pointers, or references
shall not be used, [...]" Here, we have a const variable (ST), but it
is NOT of integral or enumeration type. And of course, in the
expression a[b], an array type decays immediately to a pointer, and
pointers are banned by the second sentence that I quoted.

The constraints on other constant expressions are even more restrictive.

Quote:
but the compilers(Borland 5.6.1 and GCC 3.2.1) do not accept it.

Is there another way to pass a single characters of a constant string
as a template parameter?

Not that I know of.

--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, Tél. : +33 (0)1 30 23 45 16

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Markus Werle
Guest





PostPosted: Mon Jun 30, 2003 5:58 pm    Post subject: Re: Single character of a constant string as template parame Reply with quote



Yuriy Sorokin wrote:

Quote:
Is there another way to pass a single characters of a constant
string as a template parameter?

If you use mpl::vector for the string representation, yes.


#include "boost/mpl/integral_c.hpp"
#include "boost/mpl/vector.hpp"
#include "boost/mpl/at.hpp"
#include "boost/mpl/int.hpp"
#include "boost/mpl/transform.hpp"
#include <iostream>


namespace mpl = boost::mpl;

// crying for template typedefs ...
template <char C>
struct Char : public mpl::integral_c<char, C>
{};


template<class> struct Digit;

template<char CH>
struct Digit<Char
{
enum { value=CH-'0' };
typedef mpl::integral_c<int, value> type;
};


template <class T>
struct DigitsOfString
{
typedef typename mpl::transform<T, Digit::type AllDigits;
template <int Pos> struct At
{
typedef typename mpl::at<AllDigits, mpl::int_::type type;
enum { value = type::value };
};
};


int main()
{
// maybe U should write a maccro similar to the TYPELIST macro
// in loki-lib (see books of Andrei Alexandrescou and sf.net)
typedef mpl::vector<
Char<'H'>,
Char<'e'>,
Char<'l'>,
Char<'l'>,
Char<'o'>,
Char<'!'> > Hello;

// do it for one element
std::cerr << (
Digit<
mpl::at::type
Quote:
::type::value
)

<< std::endl;

// do it for all elements:
// (result is mpl::vector )
typedef mpl::transform<Hello, Digit::type AllDigits;
std::cerr << mpl::at::type::value << 'n';


// delegate (my favourite!)
std::cerr << DigitsOfString }


Markus

--

Build your own Expression Template Library with Daixtrose!
Visit http://daixtrose.sourceforge.net/

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.