From -1717768102805998592
X-Google-Thread: f78e5,c9a289c55d59e43d,start
X-Google-Attributes: gidf78e5,public
X-Google-Language: ENGLISH,ASCII-7-bit
Path: g2news2.google.com!news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed-east.nntpserver.com!nntpserver.com!statler.nntpserver.com!news.alt.net!comp-std-cpp-robomod!not-for-mail
From: "werasm" <w_erasm@telkomsa.net>
Newsgroups: comp.std.c++
Subject: non-type template parameter in the new std.
Date: Wed, 12 Jul 2006 10:19:14 CST
Organization: http://groups.google.com
Lines: 62
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
Message-ID: <1152697717.720355.314370@35g2000cwc.googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
X-Trace: posting.google.com 1152697722 28623 127.0.0.1 (12 Jul 2006 09:48:42 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Wed, 12 Jul 2006 09:48:42 +0000 (UTC)
Return-Path: <devnull@stump.algebra.com>
X-Authentication-Warning: mulga.csse.unimelb.edu.au: fjh set sender to devnull@stump.algebra.com using -f
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-Original-To: std-c++@mailman.ucar.edu
Delivered-To: std-c++@mailman.ucar.edu
User-Agent: G2/0.2
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; af-ZA; rv:1.7.12) Gecko/20050919 Firefox/1.0.7,gzip(gfe),gzip(gfe)
Complaints-To: groups-abuse@google.com
Injection-Info: 35g2000cwc.googlegroups.com; posting-host=196.11.195.197;
   posting-account=aj6fsQ0AAAC896enuNVrPkdD99pBtQ3W
X-Virus-Scanned: amavisd-new at ucar.edu
X-Virus-Scanned: amavisd-new at csse.unimelb.edu.au
X-Virus-Scanned: amavisd-new at csse.unimelb.edu.au
Xref: g2news2.google.com comp.std.c++:2684

Hi all,

I've had the thought of writing a class for simple range checking. The
idea would be to make ranges explicit in the argument (part of the
argument type, so to speak) of a function call and to catch range
errors before the actual function is called (early).

I admit making ranges explicit in the interface (or type) is not always
advisable, but it has its advantages for domain specific cases, such as
compass angle.

Here then, is what I came up with (I know it doesn't compile):

template <T min, T max>
class in_range
{
public:
  in_range(): v_( min ){ }
  in_range( T value ): v_( check(value) ){ }
  operator T() const{ return v_; }

private:
  T& check( T& v )
  {
    if( v < min || v > max )
    {
      throw std::out_of_range( "some formatted msg..." );
    }
    return v;
  }
  T v_;
};

It would typically be used as:

void convertCompassToShip( in_range<0,360> value );

My questions are:

1) Are floating point numbers non-type template parameters in the
latest standard.
2) Why is it necessary to specify the type when the type can be derived
from the literal for nontype template arguments e.g.

in_range needs to be specified as in_range<int,0,360>, while it can
clearly be seen (or maybe not?) that the literal already has type
<int>.

Therefore - does the new standard allow for:

template <T v1, T v2> as opposed to template <T, T v1, T v2>.

Thank you,

Werner

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]



