Ejemplo n.º 1
0
int main()
{
	unsigned long atomicValue = 4;
	unsigned long retValue = atomic(&atomicValue, 7);

	A fourAs[4] = {1, 2, 3, 4};

	a.inc();
	a.get();

	ptrA = getNewA();
	ptrA = getNewA();

	ptrA->inc();

	toBSS++;

	/* StackCopy Objekt wird aus lokalem Stack-Frame von getStackCopy in den aktuellen Stack-Frame kopiert */
	/* ab 3 Rückgabewerte erfolgt die Rückgabe über den Stack und nicht mehr über Register */
	/* ab dem 8. Parameter werden diese ebenfalls über den Stack übergeben und nicht mehr ausschließlich über Register */
	const StackCopy& stackCopy = getStackCopy();
	/* Error --> Veränderung eines temporären Objekts ist fehleranfällig und deshalb verboten */
	// StackCopy& stackCopy = getStackCopy();
	const RegCopy& regCopy = getRegCopy();
	int ret4 = return4();
	B b = getB();

	int terminator = 0x1234567;

	return 0;
}
Ejemplo n.º 2
0
int main(int argc, const char * argv[])
{
    A a;
    a.show();
    
    cout<<"------------------"<<endl;
    
    A b = a;//调用时机1:用同类型对象生成对象
    b.show();
    //A b;//这种跟上面是不一样的
    //b = a;
    //b.show();
    
    cout<<"------------------"<<endl;
    
    showA(a);//调用时机2:给非引用类型的函数参数传参时
    showA2(a);//引用类型做参数可防止调用构造函数
    
    cout<<"------------------"<<endl;

    getA(a);//调用时机3:在函数中返回非引用类型的返回值时
    
    cout<<"------------------"<<endl;
    
    const A a1;
    a1.show();//const对象只能调用const函数
    showA2(a1);//该函数的参数如果不带const会报错,因为传递的参数是常对象
}
Ejemplo n.º 3
0
int main(){
    const A a;
    a.f();
    B b;
    b.f();
    
    unique_ptr<B> pb(new B);
    cout<<pb.get()<<endl;
    
    shared_ptr<B> pb3(new B);
    cout<<" "<< pb3.get() <<endl;
    pb3.get()->f();
    
    //c++函数对象, <int (int, int) > 是一个函数签名
    function<int (int, int)> fadd = Add;
    
    cout<<fadd(1,2)<<endl;
    
    //把 fadd 函数的第一个参数绑定为1。返回一个 int (int) 类型的参数
    function<int (int) > fadd1 = bind(fadd, _1, 1);
    cout<<fadd1(100)<<endl;
    
    
    auto t1 = make_tuple(100, 200, 300);
    cout<<get<1>(t1)<<endl;
    ////下面这个会在编译器 报错误
    //cout<<get<3>(t1)<<endl;
    
    // 固定大小的容器
    array<int, 10> array1;
    cout<<array1[1]<<endl;
    
    return 0;
}
Ejemplo n.º 4
0
void bar() {
  const A a;
  int* pa = a.fooWrong(true);
  *pa = 10;
  pa = a.fooWrongWithLoop(0);
  pa = a.fooRight();
};
Ejemplo n.º 5
0
  void f() {
    std::move(a);
    a.foo();

    std::move(static_a);
    static_a.foo();
  }
Ejemplo n.º 6
0
int main( )
{
	A obj1(1,2);				//A类对象
	obj1.fun( );
	const A obj2(3,4);			//A类常对象
	obj2.fun( );
}
Ejemplo n.º 7
0
int main(){
    A a(1);
    a.f();
    a.g();
    const A b;
    b.f();
//    b.g(); // compile error because g() is not const function
}
Ejemplo n.º 8
0
int main(int argc, char **argv)
{
	const A a;
	a.TestMutable();
	a.TestMutable();
	a.TestMutable();
	return 0;
}
Ejemplo n.º 9
0
 void m_fn1(R &p2) {
   R d, e, b;
   v(c2a, p2);
   e = v(c2a, b);
   ap.m_fn2(e);
   v(c2a, p2);
   d = v(c2a, b);
   ep.m_fn2(d);
 }
int main(int argc, char**argv){
A a;
a.setA(42);
cout<<"Value of a is : "<<a.getA()<<endl;
const A b = a;
cout<<"Value of b is : "<<b.getA()<<endl;

return 0;
}
void
B::fn3() {
  int b = fld1.fn2() / 8;
  unsigned char *c = fld1.fn1(), *d = &fld3, *e = c;
  for (; a < fld2;)
    for (int j = 0; j < b; j++)
      *d++ = e[j];
  for (; 0 < fld2;)
    for (int j = 0; j < b; j++)
      e[j] = *d++;
  for (; fld2;)
    ;
}
Ejemplo n.º 12
0
int main()
{
    typedef ex::polymorphic_allocator<void> A;
    {
        A const a;
        static_assert(
            std::is_same<decltype(a.resource()), ex::memory_resource*>::value
            , ""
        );
    }
    {
        ex::memory_resource * mptr = (ex::memory_resource*)42;
        A const a(mptr);
        assert(a.resource() == mptr);
    }
    {
        A const a(nullptr);
        assert(a.resource() == nullptr);
        assert(a.resource() == nullptr);
    }
    {
        A const a;
        assert(a.resource() == ex::get_default_resource());
    }
    {
        ex::memory_resource * mptr = (ex::memory_resource*)42;
        ex::set_default_resource(mptr);
        A const a;
        assert(a.resource() == mptr);
        assert(a.resource() == ex::get_default_resource());
    }
}
Ejemplo n.º 13
0
namespace CheckDependentNonTypeParamTypes {
  template<template<typename T, typename U, T v> class X> struct A {
    void f() {
      X<int, void*, 3> x; // expected-error {{does not refer to any declaration}}
    }
    void g() {
      X<int, long, 3> x;
    }
    void h() {
      // FIXME: If we accept A<B> at all, it's not obvious what should happen
      // here. While parsing the template, we form
      //   X<unsigned char, int, (unsigned char)1234>
      // but in the final instantiation do we get
      //   B<unsigned char, int, (int)1234>
      // or
      //   B<unsigned char, int, (int)(unsigned char)1234>
      // ?
      X<unsigned char, int, 1234> x;
      int check[x.value == 1234 ? 1 : -1];
    }
  };

  template<typename T, typename U, U v> struct B { // expected-note {{parameter}}
    static const U value = v;
  };

  // FIXME: This should probably be rejected, but the rules are at best unclear.
  A<B> ab;

  void use() {
    ab.f(); // expected-note {{instantiation of}}
    ab.g();
    ab.h();
  }
}
Ejemplo n.º 14
0
int main()
   {
     int i = 42;

  // This forces A::foo(T) to be instantiated
     a.foo(i);
   }
Ejemplo n.º 15
0
int main() {
    volatile A aVolatile;
    A aNonVolatile;
    const    A aConst      = A();
    A aNonConst;
    aVolatile.volatile_func();
    //aVolatile.non_volatile_func();
    aNonVolatile.volatile_func();
    aNonVolatile.non_volatile_func();

    aConst.const_func();
    //aConst.non_const_func();
    aNonConst.const_func();
    aNonConst.non_const_func();
    return 0;
}
Ejemplo n.º 16
0
void B::start()
{
    cout << "B::start" << endl;
    aA = new A();
    aA->aB = this;
    aA->doIt();
    cout << "exit: B::start" << endl;
}
int main ()
{
  x.l = &i;
  f = x.baz();
  if (f->h.c != f->i.g.c || f->h.d != f->i.g.d)
    return 1;
  return 0;
}
Ejemplo n.º 18
0
 AllocatorBlock allocate(size_t n) {
     if (n == s && root_) {
         AllocatorBlock b = { root_, n };
         root_ = root_->next;
         return b;
     } else {
         return parent_.allocate(n);
     }
 }
		void j()
		{
			g(10); // as we see this is hided by this scope's g that take no parameter 
			// how do we overcome name hidding?
			// using declaration!!
			X x;
			f(x); // But what about this ??, with the Koenic Lookup the name hidding is fixed by Koenic Lookup
			// no needing to use using declaration for function A::f;
		}
Ejemplo n.º 20
0
int main(void)
{
    A not_const_a;
    not_const_a.say_hello();

    const A const_a;
    const_a.say_hello();

    MethodOnlyConst not_const_b;
    not_const_b.say_hello(); // using const method

    const MethodOnlyNotConst const_b;
    const int &a = 10;
    const_cast<int&>(a);
//    const_b.say_hello(); // error, passing ‘const MethodOnlyNotConst’ as ‘this’ argument of ‘void MethodOnlyNotConst::say_hello()’ discards qualifiers
//    const_cast<MethodOnlyNotConst>(const_b);

    return 0;
}
Ejemplo n.º 21
0
 void deallocate(AllocatorBlock b) {
     if (b.length != s) {
         parent_.deallocate(b);
         return;
     } else {
         auto p = (Node*)b.ptr;
         p->next = root_;
         root_ = p;
     }
 }
Ejemplo n.º 22
0
int main(){

    int a=1;
    int b=2;

    cout<<"aha"<<endl;
    cout<< (a==b) <<endl;

    cout<< SingletonStatic::getInstance() <<endl;

    A a_1 = A(5);
    const A a_2 = A(10);
    a_1.print();
    a_2.print();

    staticadd();
    staticadd();
    cout<<staticadd()<<endl;
}
Ejemplo n.º 23
0
static void const_use_in_return_test()
{
class A{
public:
	A():num(2){}
	~A(){}

	/*
	 * const here means (const this)
	 */
	void setNum(int num) const {this->num = num;}
	int getNum() { return this->num;}
	const int getNum() const { return this->num << 2;}
private:
	mutable int num; // can be modified in const context
};

class B {
public:
	B(){}
	~B(){}

	const A* get() { return new A(); }
};

	A a;
	a.setNum(100);
	int x = a.getNum();
	const A a2;
	a2.setNum(100);
	const int y = a2.getNum();
	std::cout << "x:" << x << "y:" << y << std::endl;
	
	/*
	 * const object can only access it's const function
	 * non-const object can access it's both const and non-const function
	 */	
	B b;
	b.get()->getNum();
	((A *)b.get())->setNum(99);
	((A *)b.get())->getNum();
	delete b.get();
}
Ejemplo n.º 24
0
int main()
{
    {
        typedef ex::polymorphic_allocator<void> A;
        static_assert(
            std::is_convertible<decltype(nullptr), A>::value
          , "Must be convertible"
          );
        static_assert(
            std::is_convertible<ex::memory_resource *, A>::value
          , "Must be convertible"
          );
    }
    {
        typedef ex::polymorphic_allocator<void> A;
        TestResource R;
        A const a(&R);
        assert(a.resource() == &R);
    }
}
int main(void)
{
    /*
        If a object is const type, it must call the const version of member function.
        When we call a member function, the compiler pass the invoking object's address
        to the member function as the initializer for the implicit parameter `this`.
        Because that `this` is const pointer to nonconst by default, we cannot use
        a ptr to const to initialize a ptr to nonconst.
        The `const` keywork solved this problem. It modify the `this` parameter's type in a
        member function, from const-ptr-to-nonconst to const-ptr-to-const.
        As a result, the member function can be called by both const and nonconst objects. (because the nonconst can be converted to const)
     */
    const A a;
//    a.DoNothing(); // error: passing ‘const A’ as ‘this’ argument of ‘void A::DoNothing()’ discards qualifiers
    a.DoNothingConst();

    A b;
    b.DoNothing(); // ok
    b.DoNothingConst(); // ok
}
Ejemplo n.º 26
0
        void test_lookup_at_nested_ns_scope()
        {
            // BP_nested_ns_scope
            std::printf("at nested ns scope: func() = %d\n", func()); // eval func(), exp: 4

            //printf("func(10) = %d\n", func(10)); // eval func(10), exp: 13
            // NOTE: Under the rules of C++, this test would normally get an error
            // because A::B::func() hides A::func(), but lldb intentionally
            // disobeys these rules so that the intended overload can be found
            // by only removing duplicates if they have the same type.
        }
Ejemplo n.º 27
0
// We handle the case correctly where the move consists of an implicit call
// to a conversion operator.
void implicitConversionOperator() {
  struct Convertible {
    operator A() && { return A(); }
  };
  void takeA(A a);

  Convertible convertible;
  takeA(std::move(convertible));
  convertible;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: 'convertible' used after it was moved
  // CHECK-MESSAGES: [[@LINE-3]]:9: note: move occurred here
}
Ejemplo n.º 28
0
int main()
{
    int const a = 10;
    const int b = 20;
    int c = 30;
    int d = 40;
    int const *p = &d;
    int* const q = &c;
    cout<<*p<<endl;
    d = 20;
    p = &c;
    cout<<*p<<endl;

    cout<<*q<<endl;
    //cout<<*q<<endl;

    A aa;
    aa.get();
    const A bb;
    bb.get();
    return 0;
}
Ejemplo n.º 29
0
int main()
{
    {
        static_assert(
            std::is_nothrow_default_constructible<ex::polymorphic_allocator<void>>::value
            , "Must me nothrow default constructible"
        );
    }
    {
        // test that the allocator gets its resource from get_default_resource
        TestResource R1(42);
        ex::set_default_resource(&R1);

        typedef ex::polymorphic_allocator<void> A;
        A const a;
        assert(a.resource() == &R1);

        ex::set_default_resource(nullptr);
        A const a2;
        assert(a.resource() == &R1);
        assert(a2.resource() == ex::new_delete_resource());
    }
}
Ejemplo n.º 30
0
void test1(){
  long a=1234;
  double b=3.14;
  complex c(10,11);
  C objc;

  printf("a=%d ::a='%c' b=%g ::b=%d\n",a,::a,b,::b);
  ::c.disp();
  c.disp();

  obja.disp();
  obja.complex::disp();

  objc.disp();
}