Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using alias declaration with VC++ 11

Following code example using alias declaration new in C++11 fails to compile with VC++ 11, update 1 in VS2012 and emits included errors. It compiles and executes without a peep with GCC 4.7.2 under MinGW on Windows 7 using g++ -std=c++11 -Wall in.cpp.

I didn't find any indications that this isn't supported. Furthermore, IntelliSense doesn't pick up any errors and displays a tooltip for cdptr type that says typedef const double *cdptr. My project is set to use v110 platform toolset and compile as C++ code.

How have I wronged Microsoft?

#include <iostream>

int main()
{
    using cdptr = const double*;

    const double pi = 3.14159;
    cdptr cdp = &pi;

    std::cout << "cdp: " << (*cdp) << std::endl;

    return 0;
}

Build output:

1>------ Build started: Project: AliasDeclTest, Configuration: Debug Win32 ------
1>  AliasDeclTest.cpp
1>f:\aliasdecltest\aliasdecltest.cpp(9): error C2143: syntax error : missing ';' before '='
1>f:\aliasdecltest\aliasdecltest.cpp(9): error C2873: 'cdptr' : symbol cannot be used in a using-declaration
1>f:\aliasdecltest\aliasdecltest.cpp(12): error C2065: 'cdptr' : undeclared identifier
1>f:\aliasdecltest\aliasdecltest.cpp(12): error C2146: syntax error : missing ';' before identifier 'cdp'
1>f:\aliasdecltest\aliasdecltest.cpp(12): error C2065: 'cdp' : undeclared identifier
1>f:\aliasdecltest\aliasdecltest.cpp(14): error C2065: 'cdp' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
like image 925
Martin Green Avatar asked Jan 08 '13 23:01

Martin Green


People also ask

How do you declare an alias?

The alias syntax The syntax for creating an alias is easy. You type the word "alias", followed by the name you want to give the alias, stick in an = sign and then add the command you want it to run – generally enclosed in single or double quotes.

What is alias declaration 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.

Is using better than Typedef?

In C++, 'using' and 'typedef' performs the same task of declaring the type alias. There is no major difference between the two. 'Using' in C++ is considered to define the type synonyms.

Is C++ an alias of C#?

C++ is not the alias of C# programming language.


2 Answers

MSVC11, even with the November CTP, does not implement using alias declarations.

You can find a table of C++11 support here.

like image 186
K-ballo Avatar answered Jan 27 '23 22:01

K-ballo


Based on this, it appears Visual Studio 2012 does not yet support type aliases.

like image 26
Yuushi Avatar answered Jan 27 '23 22:01

Yuushi