Why can't I declare templated type aliases inside of functions?

Trevor Hickey

Why can't I declare a templated type alias inside of a function?

#include <vector>

int main(){

    //type alias deceleration:
    template <typename T>
    using type = std::vector<T>;

    //type instantiation:
    type<int> t;

}

error: a template declaration cannot appear at block scope

Why are we forced to put these declarations outside of block scope?

#include <vector>

//type alias deceleration:
template <typename T>
using type = std::vector<T>;

int main(){

    //type instantiation:
    type<int> t;
}
R Sahu

The standard says so.

From the C++11 Standard (emphasis mine):

14 Template

2 A template-declaration can appear only as a namespace scope or class scope declaration. In a function template declaration, the last component of the declarator-id shall not be a template-id. [ Note: That last component may be an identifier, an operator-function-id, a conversion-function-id, or a literal-operator-id. In a class template declaration, if the class name is a simple-template-id, the declaration declares a class template partial specialization (14.5.5). —end note ]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

why can't we declare functions inside a structure?

Why can't I declare an array of functions in Java?

Why can't I declare dynamic variable inside protocol

why can't I declare a global variable inside an "exec" string

How can I declare a templated class type as a member method, *not* a property?

Aliases, templated functions and specialization

Why Can't I Call Two Aliases With ";"?

Why do I not have to forward declare functions or variables inside classes?

ElasticSearch: why can't I search inside specific type?

why i can't use javascript function created inside jquery object and how to declare custom function in jquery?

Why can't I declare a type of friend function by template parameter but can with alias

c++ type_traits: Why can't I call templated static methods from another template / enable_if line?

Why can't I inline-define a non-templated friend within a templated class?

Why can't I declare a type with both template container and template contained value?

Why can't I declare a shared variable in the same package as the protected type?

Can I declare a record positional parameter with a type defined inside the record?

Why can't I use column aliases in the next SELECT expression?

If I have a type defined as a string constant, why can't I pass that type to functions as an actual string?

Why can't we declare a variable of type void?

Why I can declare a const reference using type-alias?

Why can I declare a variable with 'static' qualifier without type? (in C)

Why can I declare a float in a header file, but not a custom struct type?

Why can't I use `declare -r` inside a function fo mark a variable readonly while `set -u` is in use?

Why can't I define inline functions inside Angular templates ? How else to do it?

Why I can't invoke function inside of another function in JS, if functions are objects?

I can't declare the string def type as "id"

Why can't the literal operator be templated normally?

Why can't I declare a variable using auto?

Java : Why can't I declare an array as a simple Object?