How does compiler determine which function to use?

texasbruce

As we know compiler determines the function by the overloaded function signature (like parameter types), but how does this one work:

v[i] = 1;

When compiler looks at these two overloaded functions:

const T& operator[](size_t i) const;
T& operator[](size_t i);

How does it determine which one to use? Does the compiler tries to use 1st one, and finds out it does not work, then it tries to use the second one?

Nawaz

If the object is non-const, the non-const version of the function is invoked (if it is available), else const version is invoked. Now see which is which:

const T& operator[](size_t i) const;   //CONST MEMBER FUNCTION
T& operator[](size_t i);               //NON-CONST MEMBER FUNCTION

An example,

void f(std::vector<int> const &v1, std::vector<int> & v2)
{
   std::cout << v1[0] << std::endl; //invokes const version    
   std::cout << v2[0] << std::endl; //invokes non-const version
}

Now when you write:

v[i] = 1; 

If I don't assume v to be std::vector, then it depends on the const-ness of the v. If v is const, then const-version will be invoked, else non-const version will be invoked (if it is available, else v will convert into const object and then const function will be invoked).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how does qmake determine the compiler to use in Makefile?

From Dev

How does the compiler determine which provider to use when using multiple Linq2 .... contexts?

From Dev

How does the linker determine which function to link to?

From Dev

How does a control determine which DataContext to use?

From Dev

How does the compiler know which entry in vtable corresponds to a virtual function?

From Dev

How does the compiler knows the function which is imported by dll?

From Dev

How does jenkins determine which local repository to use?

From Dev

How does jenkins determine which local repository to use?

From Dev

How does `GROUP BY` determine which aggregation method to use?

From Dev

How to use the argument of a function to determine which class to call?

From Dev

How to use the argument of a function to determine which class to call?

From Dev

How does the compiler determine types in sml

From Dev

How does the compiler determine when is it safe to RVO?

From Dev

How does compiler determine the size of a bitfield struct?

From Dev

How does the compiler determine when is it safe to RVO?

From Dev

How does the gcc determine stack size the function based on C will use?

From Dev

How does the compiler know which type to return

From Dev

How does the compiler know that which overload of std::forward function has to be called?

From Dev

How to determine which widget triggered the slot function?

From Dev

How to determine which R package contains a function?

From Dev

How to determine which button called a function?

From Dev

How does ASP.net web API determine which function to call?

From Dev

How to determine which driver to use for JDBC

From Dev

How to determine which lightdm greeter is in use?

From Dev

How to determine which GC I use?

From Dev

How to determine which lightdm greeter is in use?

From Dev

How to determine which driver to use for JDBC

From Dev

How does Xcode determine which build config to use with custom build configs and multiple projects?

From Dev

Compiler does not allow to use exit() function in C

Related Related

  1. 1

    how does qmake determine the compiler to use in Makefile?

  2. 2

    How does the compiler determine which provider to use when using multiple Linq2 .... contexts?

  3. 3

    How does the linker determine which function to link to?

  4. 4

    How does a control determine which DataContext to use?

  5. 5

    How does the compiler know which entry in vtable corresponds to a virtual function?

  6. 6

    How does the compiler knows the function which is imported by dll?

  7. 7

    How does jenkins determine which local repository to use?

  8. 8

    How does jenkins determine which local repository to use?

  9. 9

    How does `GROUP BY` determine which aggregation method to use?

  10. 10

    How to use the argument of a function to determine which class to call?

  11. 11

    How to use the argument of a function to determine which class to call?

  12. 12

    How does the compiler determine types in sml

  13. 13

    How does the compiler determine when is it safe to RVO?

  14. 14

    How does compiler determine the size of a bitfield struct?

  15. 15

    How does the compiler determine when is it safe to RVO?

  16. 16

    How does the gcc determine stack size the function based on C will use?

  17. 17

    How does the compiler know which type to return

  18. 18

    How does the compiler know that which overload of std::forward function has to be called?

  19. 19

    How to determine which widget triggered the slot function?

  20. 20

    How to determine which R package contains a function?

  21. 21

    How to determine which button called a function?

  22. 22

    How does ASP.net web API determine which function to call?

  23. 23

    How to determine which driver to use for JDBC

  24. 24

    How to determine which lightdm greeter is in use?

  25. 25

    How to determine which GC I use?

  26. 26

    How to determine which lightdm greeter is in use?

  27. 27

    How to determine which driver to use for JDBC

  28. 28

    How does Xcode determine which build config to use with custom build configs and multiple projects?

  29. 29

    Compiler does not allow to use exit() function in C

HotTag

Archive