Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some more C++11 tests... #556

Merged
merged 1 commit into from
Jul 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions cxx-squid/src/test/resources/parser/own/C++11/attributes.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
int [[attr1]] i [[attr2, attr3]];
int [[attr4]] a [10] [[attr5]];

[[ noreturn ]] void f() {
throw "error";
}

[[attr1]] void [[attr2]] func(int [[attr3]] p) [[attr4]]
{
//@todo
// [[attr4(arg1, arg2)]] if (cond)
// {
// [[vendor::attr5]] return i;
// }
}

//@todo
//[[attr1]] class C [[attr2]]
//{
//
// C::C [[attr6]] () [[attr7]];
//
//} [[attr3]] c [[attr4]], d [[attr5]];
// attr1 applies to declarator-ids c, d
// attr2 applies to the definition of class C
// attr3 applies to type C
// attr4 applies to declarator-id c
// attr5 applies to declarator-id d

[[attr1]] int [[attr2]] * [[attr3]] ( * [[attr4]] * [[attr5]] f [[attr6]] ) ( ) [[attr7]], e[[attr8]];
// attr1 applies to the pointer-to-pointer to function f, and to e
// attr2 applies to the return type of int
// attr3 applies to the return type *
// attr4 applies to the first * in the pointer-to-pointer to f
// attr5 applies to the second * in the pointer-to-pointer to f
// attr6 applies to the function variable f
// attr7 applies to the function (**f)()
// attr8 applies to e
9 changes: 9 additions & 0 deletions cxx-squid/src/test/resources/parser/own/C++11/auto.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
auto i = 0; // i is an 'int'
const auto d = 0.0; // d is a 'const double'
auto b = bind( &func, _1, _2, 13 ); // 'b' cannot (portably) be declared!

auto test()
{
for ( auto it = vec.begin() ; it != vec.end() ; ++it ) {}
return true;
}
31 changes: 31 additions & 0 deletions cxx-squid/src/test/resources/parser/own/C++11/const-expr.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
#include <iostream>
#include <stdexcept>

// Create an array of 12 integers. Legal C++11
constexpr int get_five() { return 5; }
int some_value[get_five() + 7];

class Point {
int px, py;
public:
constexpr Point() : px(0), py(0) {}
constexpr Point( int x, int y ) : px(x), py(y) {}
constexpr int x() const { return px; }
constexpr int y() const { return py; }
};
class Rect {
int x1, y1, x2, y2;
public:
constexpr Rect() : x1(0), y1(0), x2(0), y2(0) {}
constexpr Rect( int x, int y, int w, int h )
: x1(x), y1(y), x2(x+w-1), y2(y+h-1) {}
constexpt Rect( const Point & p1, const Point & p2 )
// ...provided std::{min,max} are constexpr...
: x1( std::min( p1.x(), p2.x() ) ),
y1( std::min( p1.y(), p2.y() ) ),
x2( std::max( p1.x(), p2.x() ) ),
y2( std::max( p1.y(), p2.y() ) ) {}
// ...
};
constexpr Rect rects[] = {
Rect( 0, 0, 100, 100 ),
Rect( Point( -10, -10 ), Point( 10, 10 ) ),
};

// The C++11 constexpr functions use recursion rather than iteration
// (C++14 constexpr functions may use local variables and loops)
constexpr int factorial(int n)
Expand Down
42 changes: 29 additions & 13 deletions cxx-squid/src/test/resources/parser/own/C++11/decl-type.cc
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
#include <iostream>
#include <vector>

struct A {
double x;
double x;
};
const A* a = new A{ 0 };

decltype(a->x) x3; // type of x3 is double (declared type)
decltype((a->x)) x4 = x3; // type of x4 is const double& (lvalue expression)

auto add_int( int a, int b ) -> int { return a + b; }

template <class T, class U>
auto add(T t, U u) -> decltype(t + u); // return type depends on template parameters

int example()
{
const std::vector<int> v(1);
auto a = v[0]; // a has type int
decltype(v[1]) b = 1; // b has type const int&, the return type of
// std::vector<int>::operator[](size_type) const
auto c = 0; // c has type int
auto d = c; // d has type int
decltype(c) e; // e has type int, the type of the entity named by c
decltype((c)) f = c; // f has type int&, because (c) is an lvalue
decltype(0) g; // g has type int, because 0 is an rvalue
}

int main()
{
int i = 33;
decltype(i) j = i * 2;
int i = 33;
decltype(i) j = i * 2;

std::cout << "i = " << i << ", "
<< "j = " << j << '\n';
std::cout << "i = " << i << ", "
<< "j = " << j << '\n';

auto f = [](int a, int b) -> int {
return a*b;
};
auto f = [](int a, int b) -> int {
return a*b;
};

decltype(f) f2 = f; // the type of a lambda function is unique and unnamed
i = f(2, 2);
j = f2(3, 3);
decltype(f) f2 = f; // the type of a lambda function is unique and unnamed
i = f(2, 2);
j = f2(3, 3);

std::cout << "i = " << i << ", "
<< "j = " << j << '\n';
std::cout << "i = " << i << ", "
<< "j = " << j << '\n';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
struct Class : public Base
{
unsigned char x;
unsigned char y;

Class ( int x )
: Base ( 123 ), // initialize base class
x ( x ), // x (member) is initialized with x (parameter)
y { 0 } // y initialized to 0
{} // empty compound statement

Class ( double a )
: y ( a+1 ),
x ( y ) // x will be initialized before y, its value here is indeterminate
{} // base class constructor does not appear in the list, it is
// default-initialized (not the same as if Base() were used, which is value-init)

Class()
try // function-try block begins before the function body, which includes init list
: Base ( 789 ),
x ( 0 ),
y ( 0 )
{
// ...
}
catch (...)
{
// exception occurred on initialization
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
struct SomeType
{
// implicit conversion
operator int() const { return 7; }

// explicit conversion
explicit operator int*() const { return nullptr; }
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class X1 {
friend C; // OK: Class C is a friend
};

class X2 {
friend Ct; // OK: class C is a friend
friend D; // error: no type-name D in scope
friend class D; // OK: elaborated-type-specifier declares new class
};

template <typename T> class R {
friend T;
};

R<C> rc; // class C is a friend of R<C>
R<int> ri; // OK: "friend int" is ignored
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extern template class std::vector<MyClass>;
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
struct Base1 final { };
struct Derived1 : Base1 { }; // ill-formed because the class Base1 has been marked final

struct A
{
virtual void foo() final; // A::foo is final
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// forward declaration of enums
enum Enum2 : unsigned int; // Legal in C++11, the underlying type is explicitly specified.
enum class Enum3; // Legal in C++11, the underlying type is int.
enum class Enum4 : unsigned int; // Legal in C++11.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class SomeType {
int number;

public:
SomeType(int new_number) : number(new_number) {}
SomeType() : SomeType(42) {}
};
19 changes: 19 additions & 0 deletions cxx-squid/src/test/resources/parser/own/C++11/inline-namespace.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace L {
inline namespace M {
inline namespace N {
/*...*/
}
}
}

// if you compile the following code the output of the resulting executable is 1
namespace A {
inline namespace B {
int foo(bool) { return 1; }
}
int foo(int) { return 2; }
}

int main(void) {
return A::foo(true);
}
51 changes: 51 additions & 0 deletions cxx-squid/src/test/resources/parser/own/C++11/lambda-function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,57 @@
#include <algorithm>
#include <functional>

void example1()
{
auto func1 = [] (int x, int y) -> int { return x + y; };
auto func2 = [] ( int x ) -> int { return f( g( x ) ); }; // -> int is optional
auto func3 = [] ( int x ) { return ( x + 1 ) * 2; };
}

// generic lambda, operator() is a template with one parameter
auto vglambda = [](auto printer) {
return [=](auto&&... ts) { // generic lambda, ts is a parameter pack
printer(std::forward<decltype(ts)>(ts)...);
return [=]{ printer(ts...); }; // nullary lambda (takes no parameters)
};
};
auto p = vglambda( [](auto v1, auto v2, auto v3) {
std::cout << v1 << v2 << v3;
} );

void example2()
{
auto q = p(1, 'a', 3.14); // outputs 1a3.14
q(); // outputs 1a3.14

// generic lambda, operator() is a template with two parameters
auto glambda = [](auto a, auto&& b) { return a < b; };
bool b = glambda(3, 3.14); // OK
}

void example3() {
float x, &r = x;
[=] { // x and r are not captured (appearance in a decltype operand is not an odr-use)
decltype(x) y1; // y1 has type float
decltype((x)) y2 = y1; // y2 has type float const& because this lambda
// is not mutable and x is an lvalue
decltype(r) r1 = y1; // r1 has type float& (transformation not considered)
decltype((r)) r2 = y2; // r2 has type float const&
};
}

struct X {
int x, y;
int operator()(int);
void f() {
// the context of the following lambda is the member function X::f
[=]()->int {
return operator()(this->x + y); // X::operator()(this->x + (*this).y)
// this has type x*
};
}
};

int main()
{
std::vector<int> c{ 1,2,3,4,5,6,7 };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,31 @@
#include <map>
#include <string>

struct Object {
float first;
int second;
};

Object scalar = {0.43f, 10}; // One Object, with first=0.43f and second=10
Object anArray[] = {{13.4f, 3}, {43.28f, 29}, {5.934f, 17}}; // An array of three Objects

class SequenceClass {
public:
SequenceClass(std::initializer_list<int> list);
};
SequenceClass some_var = {1, 4, 5, 6};

struct Foo {
std::vector<int> mem = { 1,2,3 }; // list-initialization of a non-static member
std::vector<int> mem2;
Foo() : mem2{ -1, -2, -3 } {} // list-initialization of a member in constructor
};

std::vector<std::string> v1 = { "xyzzy", "plugh", "abracadabra" };
std::vector<std::string> v2({ "xyzzy", "plugh", "abracadabra" });
std::vector<std::string> v3{ "xyzzy", "plugh", "abracadabra" }; // see "Uniform initialization" below


std::pair<std::string, std::string> f(std::pair<std::string, std::string> p)
{
return{ p.second, p.first }; // list-initialization in return statement
Expand Down
12 changes: 12 additions & 0 deletions cxx-squid/src/test/resources/parser/own/C++11/long-long-int.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
long long i = 0;
long long int j = 0;

void func1(long long p) {}
void func2(long long int p) {}

long long func3() { return 0; }
long long int func4() { return 0; }

//@todo
//auto func5(long long p) -> long long { return 0; }
//auto func6(long long int p) -> long long int { return 0; }
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
struct SomeType {
SomeType() = default; //The default constructor is explicitly stated.
SomeType(OtherType value);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// The following type is non-copyable:
struct NonCopyable {
NonCopyable() = default;
NonCopyable(const NonCopyable&) = delete;
NonCopyable & operator=(const NonCopyable&) = delete;
};

// The = delete specifier can be used to prohibit calling any function, which can be used to disallow calling a member function with particular parameters. For example:
struct NoInt {
void f(double i);
void f(int) = delete;
};

// This can be generalized to disallow calling the function with any type other than double as follows:
struct OnlyDouble {
void f(double d);
template<class T> void f(T) = delete;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
char* txt1 = u8"I'm a UTF-8 string.";
char16_t* txt2 = u"This is a UTF-16 string.";
char32_t* txt3 = U"This is a UTF-32 string.";

char* txt1 = u8"This is a Unicode Character: \u2018.";
char16_t* txt2 = u"This is a bigger Unicode Character: \u2018.";
char32_t* txt3 = U"This is a Unicode Character: \U00002018.";
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class SomeClass {
public:
SomeClass() {}
explicit SomeClass(int new_value) : value(new_value) {}

private:
int value = 5;
MyWidget * m_myWidget = 0;
double d = 1.0;
};
Loading