Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ concepts lite and type alias declaration

Is it possible to use typedef or using to declare a type alias inside a concept, as proposed by the Concepts TS? If I try something like the following MWE, the code does not compile (with gcc 6.2.1 and the -fconcepts switch)

#include <type_traits>

template<typename T>
concept bool TestConcept ()
{
    return requires(T t)
    {
        using V = T;
        std::is_integral<V>::value;
    };
}

int main()
{
    return 0;
}

Resulting error:

main.cpp: In function ‘concept bool TestConcept()’:
main.cpp:8:9:  error: expected primary-expression before ‘using’  
         using V = T;  
         ^~~~~   
main.cpp:8:9: error: expected ‘}’ before ‘using’
main.cpp:8:9: error: expected ‘;’ before ‘using’
main.cpp:4:14: error: definition of concept ‘concept bool TestConcept()’ has multiple  statements
 concept bool TestConcept ()  
              ^~~~~~~~~~~ 
main.cpp: At global scope:
main.cpp:11:1: error: expected declaration before ‘}’ token
 } 
 ^
like image 315
Slizzered Avatar asked Oct 30 '16 19:10

Slizzered


People also ask

What is type alias in C?

typedef is a reserved keyword in the programming languages C and C++. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type.

What is an alias type?

Type alias is a name that refers to a previously defined type (similar to typedef). Alias template is a name that refers to a family of types.

What is the meaning of alias in C++?

You can use an alias declaration to declare a name to use as a synonym for a previously declared type. (This mechanism is also referred to informally as a type alias). You can also use this mechanism to create an alias template, which can be useful for custom allocators.

What is the function of typedef in C++?

The typedef keyword allows the programmer to create new names for types such as int or, more commonly in C++, templated types--it literally stands for "type definition". Typedefs can be used both to provide more clarity to your code and to make it easier to make changes to the underlying data types that you use.


1 Answers

No. According to the concepts TS, a requirement is:

requirement:
    simple-requirement
    type-requirement
    compound-requirement
    nested-requirement

Where a simple-requirement is an expression followed by a ; and a type-requirement is something like typename T::inner. The other two sound like what the name suggests.

A type alias is a declaration, not an expression, and so does not meet the requirement of a requirement.

like image 132
Barry Avatar answered Oct 10 '22 23:10

Barry