Why is initialization of a constant dependent type in a template parameter list disallowed by the standard?

{ edited by mod to shorten lines to ~70 characters. -mod }

Hello all,

This question has been bugging me for a while and I have posted it as
a question on SO here:


Which basically says the following:

In the answer to this post "(Partially) specializing a non-type template
parameter of dependent type"
it quotes from the standard paragraph [temp.class.spec] 14.5.5/8:

> The type of a template parameter corresponding to a specialized non-type
> argument shall not be dependent on a parameter of the specialization. [
> Example:
> > template <class T, T t> struct C {};
> > template <class T> struct C<T, 1>; // error
> > 
> > template< int X, int (*array_ptr)[X] > class A {};
> > int array[5];
> > template< int X > class A<X,&array> { }; // error
> --end example ]

My question is why is this restriction here? There is at least one use
case where I find that this restriction interferes with writing clean
code. E.g.

> template <typename T, T*>
> struct test;
> template <typename T>
> struct test<T, nullptr> // or struct test<T, (T*)nullptr>
> {
> };
> template <typename R, typename...ARGs, R(*fn)(ARGs...)>
> struct test<R(ARGs...), fn>
> {
> };

Though I'm unsure if there are other cases that stating a constant based
on a type is a problem beyond not making any sense, but yet, if any such
issue did exist, it could probably still have the same problem as using
std::integral_type workaround.

Anyone know the reason for why this is so?

NOTE: Nobody on SO seems to know what the reason is for this restriction,
though there was a comment that it may have originated from N0668, but no
rationale has yet been found.  So I've decided to come here to the harder
core group. ;)



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

9/3/2015 4:08:41 PM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

0 Replies

Similar Articles

[PageSpeed] 26


Similar Artilces:

Typestring lib
https://github.com/irrequietus/typestring it seems pretty interesting :-) not to be confused with constexpr string literals. there strings are real types! pasting the readme file: Strings as C++ types for use in template parameter lists. What this is all about Template metaprogramming is for those who enjoy using a pure, non-strict, f= unctional programming language, with pattern matching within C++. If there = is anything truly worth in the language that's the completely serendipitous= emergence of this important idiom to the point of it being a core practice= for lot...

Templates: Type-dependent constants
Hi all. I am working with a class which applies numerical algorithms to numbers of type either 'float' or 'double'. In such applications one needs to handle various type-dependent constants, like minimum/maximum values, numerical accuracy for answers after computations, etc. For the type 'float', the relative accuracy is one the order 1e-8, for 'double' it is on the order 1e-15. How can I design a template class which handles these sorts of type-dependent constants seamlessly? The idea is described as: ///////////////////////////////////////////////////////...

Can a (non-static) data member use "Type{Whatever}" in its brace/list initializer when "Type" is a C-level array?
I have code like (skipping the base-case specialization): template < typename Number, std::size_t Rank > struct complex_rt { using size_type = std::size_t; static constexpr size_type rank = Rank; using value_type = Number; //... template < typename T, size_type R, typename ...U, typename = typename std::enable_if<(R < rank - 1u)>::type, typename = typename std::enable_if<((1u + sizeof...( U )) <= (1ULL << ( rank - R )))>::type, typename ...

Template Parameter List non-type
I would like one of my template parameters to be an std::string. I realize this is a non-type and non-integral value. What options are available to me? template <class T, string S> // not allowed class A {...}; I can get this to compile as follows: template <class T, const string& S> class A {...}; string s("some string"); A<int, s> a; However, if my definition of s is: const string s("some string"); It will not compile... unfortunately, I am working with a lot of previously defined const strings. I imagine there is an elegant solution to this ...

Initializer lists and default template parameters
struct cstring_data { unsigned size; unsigned capacity; unsigned refs; }; template<size_t N> struct cstring { cstring_data data; char str[N]; }; #define cstring(STR) \ {{sizeof(STR), sizeof(STR), 0}, STR} void cstring_test() { static const cstring<4> value = cstring("hmm"); } Is there any way to get identical syntax but drop the need to specify '<3>'? I tried several methods (i.e. default template parameter, subclassing), all of them failed to compile. [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moder...

Template specialization for templated and primitive type template parameters
Can anyone help with the following, I don't know if it's possible, but I'm certain there must be a standard way of dealing with this. I have the following: template<typename FooBar, typename Foo> class Bar { private: Foo foo; } Now I want to be able to handle when 'Foo' is a primitive type, or more specifically a primitive pointer type, like 'char *' and also when Foo is itself a template class: template<typename FooBar, template<T> class Foo> class Bar { private: Foo<FooBar> foo; } All the member functions and other fields of Bar are ...

Must variadic template parameters occur at the end of the template parameter list?
Is this valid in draft C++0x? template<typename ... Types1, typename ... Types2> class Foo; gcc 4.4.1 both says no and says why: vartemp.cpp:33: error: parameter pack 'Types1' must be at the end of the template parameter list I can't find wording to back up this restriction, and in fact I find the following in of N3000: template<class... TTypes, class... UTypes> bool operator==(const tuple<TTypes...>& t, const tuple<UTypes...>& u); Is this a bug in gcc or a part of the draft standard that requires amendment? If it...

List of implementation dependent parts of C standard
Hi, Referring to C90, is there somewhere published a list of all parts of the standard where the behavior is not specified or implementation dependent? If not and I want to find all such parts I could search a pdf version of the standard for keywords such as "implementation dependent" to highlight the section. Can anybody suggest a complete list of such keywords to make sure I find all the relevant sections? Thank you. embeddedc@hotmail.com wrote: > Referring to C90, is there somewhere published a list of all parts of > the standard where the behavior is not specified or...

standard C operator as parameter to template function
Dear newsgroup, I want to call a template function with a standard operator (+, /, -, * for double) as parameter. Here is a minimal example: template <typename T> T opIf(T val1, T val2, T (*op)(T,T) ) { if (val2 != 0) return op(val1, val2); else return (T)0; } How can I call this function with e.g. the standard / operator? double res = opIf(10., 5., operator/); or similar does not work. Thanks a lot, Klaus -- Please insert my name and a dot before my email address. On Fri, 22 Jun 2007 11:31:11 +0200, Klaus Schneider wrote: > Dear newsgroup, > > I wan...

Templates rejected on non-dependent parameters before deduction in C++0X?
(I attempted to post this to comp.std.c++ but it didn't seem to pass moderation, so I thought this is a 2nd best location) While doing some meta-programming for choosing a return type of an overloaded function, I ran into a problem with the expansion of templates in the return type of overloaded functions (that are rejected in the overload for other reasons). A version at the bottom is attached, though a link on intel simplifies the problem a little. The code works fine in g++/MinGW (I am using 4.4). However, on Intel 11.1 and MSVC10, it fails since it tries to instantiate ...

Deduction of not-type template parameter type
Sometimes it would be convenient to allow a function argument to be used as a template argument. For example, consider a factory function that generates lookup functions (the example is inspired by James Kanze's thread "Incomplete type in template parameter, complete type as argument"): typedef bool (*contains_fun) (const char* key); template <size_t N, const char* (&Keys) [N]> bool contains (const char* key) { return std::find_if (Keys, Keys + N, Match (key)) != Keys + N; } template <size_t N> contains_fun make_contains ...

Template template parameters in C++0x
One of the weaknesses of template template parameters in C++03 is that they only accept class templates and not function templates to be passed as the template argument. In C++0x, with the addition of template alias, the documentation for a template template parameter has changed to allow a class template or a template alias to be passed for the template template parameter. I have a few questions about this which the documentation in 14.3.3 does not make clear to me: 1) Can a function template as a template alias be passed as the template argument for a template template param...

Comeau vs gcc
The code below compiles fine on Comeau under all the versions available on http://www.comeaucomputing.com/tryitout/ However when compiling with gcc (4.3.2), the following error occurs: :9: error: expected constructor, destructor, or type conversion before 'Foo' It can be easily fixed by adding in a 'typename' before the return type on line 9 such that it is: typename Foo<T>::return_type Foo<T>::bar() { However since this works with Comeau, is this a case of gcc being too strict? is this a bug in gcc? -------------------------------------------------- template <...

Forward-declared template parameters and non-dependent member types
Let's say I have a container class which is implemented something like this (unimportant parts removed): template <typename T> class Container { public: typedef std::ptrdiff_t difference_type; typedef T* pointer; private: T data_; }; Container<T>::difference_type does not depend on T in any way. Container<T>::pointer depends on T, but does not require a full definition of T. However, it appears to be illegal to do this: struct MyType; // note: forward-declaration only typedef Container<MyType>::difference_type difference_type; // Fails typedef Con...

Web resources about - Why is initialization of a constant dependent type in a template parameter list disallowed by the standard? - comp.lang.c++.moderated

Initialization vector - Wikipedia, the free encyclopedia
is a fixed-size input to a cryptographic primitive that is typically required to be random or pseudorandom . Randomization is crucial for encryption ...

Booting an Intel Architecture System, Part I: Early Initialization
The boot sequence today is far more complex than it was even a decade ago. Here's a detailed, low-level, step-by-step walkthrough of the boot ...

Patent US6061520 - Method and system for performing static initialization - Google Patents
... consolidating class files, the preloader identifies all &lt;clinit&gt; methods and play executes these methods to determine the static initialization ...

Tisdale on model initialization in wake of the leaked IPCC draft
Should Climate Models Be Initialized To Replicate The Multidecadal Variability Of The Instrument Temperature Record During The 20th Century? ...

Spring Environment is for initialization code only - Java Code Geeks
Since version 3.1, the Spring framework offers an abstraction towards several different sources through which you can easily configure your application: ...

Ball Invasion for iPad2 for iPad on the iTunes App Store
Get Ball Invasion for iPad2 on the App Store. See screenshots and ratings, and read customer reviews.

Stack Overflow
Stack Exchange log in - careers - chat - meta - about - faq Questions Tags Users Badges Unanswered Ask Question Top Questions interesting 325 ...

IBM System/360 - Wikipedia, the free encyclopedia
. It was the first family of computers designed to cover the complete range of applications, from small to large, both commercial and scientific. ...

ESANN - Author index
Electronic preceedings author index ESANN - European Symposium on Artificial Neural Networks [ Electronic proceedings home page ] A B C D E F ...

Using event capturing to improve Basecamp page load times
... would be best if we could avoid initializing sortables until the last possible moment, just before you start to drag a to-do. Deferring Initialization ...

Resources last updated: 1/25/2016 6:49:16 AM