Commit 13a513d6 authored by Ralf Habacker's avatar Ralf Habacker
Browse files

Add c++11 code import test cases.

CCBUG:338649
parent 368c43e0
// https://en.wikipedia.org/wiki/C%2B%2B11#Alternative_function_syntax
// #1
template<class Lhs, class Rhs>
auto adding_func(const Lhs &lhs, const Rhs &rhs) -> decltype(lhs+rhs) {return lhs + rhs;}
// #2
struct SomeStruct {
auto func_name(int x, int y) -> int;
};
auto SomeStruct::func_name(int x, int y) -> int {
return x + y;
}
cxx11-constexpr.h
\ No newline at end of file
// https://en.wikipedia.org/wiki/C%2B%2B11#constexpr_.E2.80.93_Generalized_constant_expressions
// #1
constexpr int get_five() {return 5;}
int some_value[get_five() + 7]; // Create an array of 12 integers. Legal C++11
// #2
constexpr double earth_gravitational_acceleration = 9.8;
constexpr double moon_gravitational_acceleration = earth_gravitational_acceleration / 6.0;
// https://en.wikipedia.org/wiki/C%2B%2B11#Explicit_overrides_and_final
// #1
struct Base {
virtual void some_func(float);
};
struct Derived : Base {
virtual void some_func(int) override; // ill-formed - doesn't override a base class method
};
// #2
struct Base1 final { };
struct Derived1 : Base1 { }; // ill-formed because the class Base1 has been marked final
// #3
struct Base2 {
virtual void f() final;
};
struct Derived2 : Base2 {
void f(); // ill-formed because the virtual function Base2::f has been marked final
};
// https://en.wikipedia.org/wiki/C%2B%2B11#Extern_template
// #1
extern template class std::vector<MyClass>;
// https://en.wikipedia.org/wiki/C%2B%2B11#Initializer_lists
// #1
class SequenceClass {
public:
SequenceClass(std::initializer_list<int> list);
};
// #2
SequenceClass some_var = {1, 4, 5, 6};
// #3
void function_name(std::initializer_list<float> list);
function_name({1.0f, -3.45f, -0.4f});
// #4
std::vector<std::string> v = { "xyzzy", "plugh", "abracadabra" };
std::vector<std::string> v({ "xyzzy", "plugh", "abracadabra" });
std::vector<std::string> v{ "xyzzy", "plugh", "abracadabra" }; // see "Uniform initialization" below
// https://en.wikipedia.org/wiki/C%2B%2B11#Lambda_functions_and_expressions
// #1
[](int x, int y) -> int { return x + y; }
// https://en.wikipedia.org/wiki/C%2B%2B11#Null_pointer_constant
// #1
char *pc = nullptr; // OK
int *pi = nullptr; // OK
bool b = nullptr; // OK. b is false.
//int i = nullptr; // error
foo(nullptr); // calls foo(nullptr_t), not foo(int);
// https://en.wikipedia.org/wiki/C%2B%2B11#Object_construction_improvement
// #1
class SomeType {
int number;
public:
SomeType(int new_number) : number(new_number) {}
SomeType() : SomeType(42) {}
};
// #2
class BaseClass {
public:
BaseClass(int value);
};
class DerivedClass : public BaseClass {
public:
using BaseClass::BaseClass;
};
// #3
class SomeClass {
public:
SomeClass() {}
explicit SomeClass(int new_value) : value(new_value) {}
private:
int value = 5;
};
// https://en.wikipedia.org/wiki/C%2B%2B11#Range-based_for_loop
// #1
int my_array[5] = {1, 2, 3, 4, 5};
// double the value of each element in my_array:
for (int &x : my_array) {
x *= 2;
}
// similar but also using type inference for array elements
for (auto &x : my_array) {
x *= 2;
}
// https://en.wikipedia.org/wiki/C%2B%2B11#Strongly_typed_enumerations
// #1
enum class Enumeration {
Val1,
Val2,
Val3 = 100,
Val4 // = 101
};
// #2
enum class Enum2 : unsigned int {Val1, Val2};
// #3
enum Enum3 : unsigned long {Val1 = 1, Val2};
// #4
//enum Enum1; // Illegal in C++03 and C++11; the underlying type cannot be determined.
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.
//enum Enum2 : unsigned short; // Illegal in C++11, because Enum2 was previously declared with a different underlying type.
\ No newline at end of file
// https://en.wikipedia.org/wiki/C%2B%2B11#Type_inference
// #1
auto some_strange_callable_type = std::bind(&some_function, _2, _1, some_object);
auto other_variable = 5;
// #2
int some_int;
decltype(some_int) other_integer_variable = 5;
// #3
for (auto itr = myvec.cbegin(); itr != myvec.cend(); ++itr) ;
// #4
for (auto& x : myvec) ;
// #5
#include <vector>
int main() {
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
}
// https://en.wikipedia.org/wiki/C%2B%2B11#Uniform_initialization
// #1
struct BasicStruct {
int x;
double y;
};
struct AltStruct {
AltStruct(int x, double y) : x_{x}, y_{y} {}
private:
int x_;
double y_;
};
BasicStruct var1{5, 3.2};
AltStruct var2{2, 4.3};
// #2
struct IdString {
std::string name;
int identifier;
};
IdString get_string() {
return {"foo", 42}; //Note the lack of explicit type.
}
// #3
std::vector<int> the_vec{4};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment