Can I use a variable template to declare another variable template?

chris

With variable templates coming in C++14 (and Clang already supporting them) and a proposal for standard is_same_v and likewise type traits, I figured being able to make new type traits as follows would be neat:

template<typename T>
constexpr bool is_const_and_volatile{std::is_const_v<T> && std::is_volatile_v<T>};

Alas, this results in errors equivalent to the following SSCCE (this one contains everything mentioned below):

#include <type_traits>

template<typename T>
constexpr bool is_pointer{std::is_pointer<T>::value};

template<typename T>
constexpr bool foo{is_pointer<T>};

int main() {
    //foo<int *>;
}

With the line in main commented, Clang spits out the following:

warning: variable is_pointer<type-parameter-0-0> has internal linkage but is not defined

It looks defined to me (note that changing T to int * in foo works fine). Uncommenting the line in main to instantiate foo gives this (again, T to int * works fine):

error: constexpr variable foo<int *> must be initialized by a constant expression

However, replacing foo with the following old syntax causes both instances to work fine:

constexpr bool foo{std::is_pointer<T>::value};

Is there something I'm missing about variable templates? Is there a way to build new variable templates with them, or am I forced to use the older syntax to build new ones and only enjoy the syntactic sugar when using them for other code?

Richard Smith

Your code is valid, and is accepted by clang SVN. The link error was caused by clang bug 17846, which I fixed a couple of days ago.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Can I use a template variable in another template in Golang?

can I use SFINAE to selectively define a member variable in a template class?

How can I use a Django template variable in HTML in a model TextField?

How can i use variable inside template literals?

How can I use a enum variable as template argument?

Can I use a template literal containing a variable as an object key?

How to declare a variable in a template in Angular

How to declare a global variable in a template?

How to declare variable in template of Angular?

Forward declare a constexpr variable template

Template variable in another variable - Grafana

How can I Declare/define/initialize a static member variable of template classes as static member variables of a class?

Why can't I declare a static constexpr variable in a template class using msvc?

How can I make a Grafana template with a variable reference another variable using Prometheus as a datasource?

Can a variable template be passed as a template template argument?

Django use value of template variable as part of another variable name

how can i increment a variable with an "if" in a django template?

Use a template to set a variable

Use variable in Ansible template

Can i use another template engine with angularjs?

How I can use a django variable in if statement of django template into a javascript string variable

Can I use a template function as another template's parameter?

Angular - Declare variable within component template

Cannot declare a variable in a variadic template using metaprogramming

get value of variable to another template

Set variable in twig template then using it with another variable

Is it possible to use a static template variable in a template function?

Use a YAML variable template into a step template

Can I declare and use a static-non-template member of a template class in C++?

TOP Ranking

HotTag

Archive