C++ language

C++ language

Using-declaration

Introduces a name that is defined elsewhere into the declarative region where this using-declaration appears.

  • Using-declaration introduces a member of another namespace into current namespace or block scope.
  • 
    #include <iostream>
    #include <string>
     
    using std::string;
     
    int main()
    {
        string str = "Example";
        using std::cout;
        cout << str;
    }    
        
  • Using-declaration introduces a member of a base class into the derived class definition

Namespaces

Namespaces provide a method for preventing name conflicts in large projects.
Symbols declared inside a namespace block are placed in a named scope that prevents them from being mistaken for identically-named symbols in other scopes.


#include <iostream>

using namespace std;

namespace X
{
    int x=1;

    void Check(){
        cout << x << endl;
        //Y::Check();  --> error: Y has not been declared
    }
}// namespace X

namespace Y
{
    int y=2;
    int Check(){
        X::Check();
        return y;
    }

}// namespace Y

using namespace std;

namespace Hello
{

struct Node{
    int val;
};

}// namespace Hello

namespace Other
{

using namespace Hello;
static Node node;

void printNodeVal ( struct Node node ){
        cout << "Hello" << endl;
}

}// namespace Other

int main() {
    cout << Y::Check() << endl;

    return 0;
}

Difference between namespace and class in C++

The namespace and classes are two different concepts.

  • Classes are datatypes.
  • Classes are basically extended version of structures.
  • Classes can contain data members and functions as members, but namespaces can contain variables and functions by grouping them into one.
  • The namespaces cannot be created as objects.
  • This concept is used as additional information to differentiate similar functions, classes, variables etc.
  • Variables, functions with same name can be placed in different namespaces.
Some important differences of namespaces and classes:
  • The namespaces are used to make a group of identifiers so they do not collide.
  • By using class, we have to create an instance of that class, but for namespaces this is not true.
  • For namespace we use ‘using’ declaration.
  • For classes it is not possible unless we are deriving from it.
  • We can reopen namespace and add more elements across translation units.
  • This cannot be done using classes.
    For ex.,
    • build success
    • 
      namespace my_namespace {
         int function1();
      }
      namespace my_namespace {
         int function1();
      }        
      namespace A {
      int f1();
      }
      
      namespace A {
      int f2();
      }
              
    • build failure
    • 
      class my_class {
         int function1();
      };
      class my_class {
         int function1();
      };        
      class A {
      	int f1();
      };
      
      class A { // illegal
      	int f2();
      };
              
    • we can make a namespace alias
    • 
      namespace foo {
          namespace bar {
              namespace baz {
                  int qux = 42;
              }
          }
      }
        
      namespace fbz = foo::bar::baz;        
              

Template

A template is a C++ entity that defines one of the following:
  • a family of classes (class template), which may be nested classes
  • a family of functions (function template), which may be member functions
  • an alias to a family of types (alias template) (since C++11)
  • a family of variables (variable template) (since C++14)
  • a concept (constraints and concepts) (since C++20)

Class template

Syntax:

template < parameter-list > class-declaration
  • class-declaration
  • a class declaration. The class name declared becomes a template name.
  • parameter-list
  • a non-empty comma-separated list of the template parameters, each of which is either a non-type parameter, a type parameter, a template parameter, or a parameter pack of any of those.

Class template instantiation

A class template by itself is not a type, or an object, or any other entity.
The template arguments must be provided so that the compiler can generate an actual class (or function, from a function template).
  • Explicit instantiation
  • 
    template class-key template-name < argument-list > ;    
        
    class-key: class, struct or union
    
    namespace N 
    {
        template<class T> 
        class Y // template definition
        { 
            void mf() {} 
        }; 
    }
     
    // template class Y<int>; // error: class template Y not visible in the global namespace
    using N::Y;
    // template class Y<int>; // error: explicit instantiation outside 
                              // of the namespace of the template
    template class N::Y<char*>;       // OK: explicit instantiation
    template void N::Y<double>::mf(); // OK: explicit instantiation    
        
  • Implicit instantiation
  • 
    template<class T> 
    struct Z // template definition
    {
        void f() {}
        void g(); // never defined
    }; 
     
    template struct Z<double>; // explicit instantiation of Z<double>
    Z<int> a;                  // implicit instantiation of Z<int>
    Z<char>* p;                // nothing is instantiated here
     
    p->f(); // implicit instantiation of Z<char> and Z<char>::f() occurs here.
            // Z<char>::g() is never needed and never instantiated:
            // it does not have to be defined
    		
    template<class T> 
    class X;    // declaration, not definition
     
    X<char> ch; // error: incomplete type X<char>    
        

Function template


template < parameter-list > function-declaration

留言

熱門文章