Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How explicit , volatile and mutable keywords works INTERNALLY in C++ ?

How explicit , volatile and mutable keywords works INTERNALLY in C++ ?

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Amrit Agr
    wrote on last edited by
    #1

    Hi Friends, I have been asked in interviews like "How explicit , volatile and mutable keywords works INTERNALLY in C++ ?" Please let me know. Rgeards, Amrit

    L L 2 Replies Last reply
    0
    • A Amrit Agr

      Hi Friends, I have been asked in interviews like "How explicit , volatile and mutable keywords works INTERNALLY in C++ ?" Please let me know. Rgeards, Amrit

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      MSDN has all the answers: User-Defined Type Conversions (C++)[^].

      1 Reply Last reply
      0
      • A Amrit Agr

        Hi Friends, I have been asked in interviews like "How explicit , volatile and mutable keywords works INTERNALLY in C++ ?" Please let me know. Rgeards, Amrit

        L Offline
        L Offline
        leon de boer
        wrote on last edited by
        #3

        Explicit and volatile you need to know, if you are seen using mutable in C++ we put you up against the wall and shoot :-) There are a couple of places you need volatile the two main ones are in multitasking code and interfacing to physical hardware. What volatile tells the compiler the result at that address can change between uses of it ... so it stops the optimizer assuming the value hasn't changed between uses of that address. Imagine the situation you have code that reads the port of a timer at an address. Then you do some other stuff not touching the port address and then you go an read the port again. Now if you are dealing with memory the two reads will have the same value as the code between the two reads didn't touch the port. So an optimizer might well conclude it can remove the second read and just hold the first read value. Only in reality the timer values ticks because it's hardware and so we need the second read to occur it can't be optimized. The same situation occurs in multitasking, memory that our own code didn't touch can change. So volatile is about making sure the optimizer doesn't shortcut stuff assuming just because the code hasn't touched it, that it hasn't changed. So any variable marked with volatile will have any action on that variable not subject to optimization. Explicit is when you want the compiler to stop being smart and convert types for you and pretending it's GWBASIC :-) It used to happen mostly on class/object code that a constructor for example would take a string as a parameter. If you gave it an integer it would automatically know to convert the integer to a string, which is pretty cool sometimes. Other times it leads to a complete and utter bug because you didn't allow for it. So if you tell the compiler you want explicit it turns off the GWBASIC parser and stops the conversion it will throw an error instead. Mutable is one you will rarely if ever need and if you do need it most of us will argue your code is badly organized or you are super lazy and dangerous. The better name for it would be cached or stencil playtime. So you have a constant or someone passes you in a constant, but you just want to change it I mean string "Drive A" is so close to "Drive B". You declare it mutable you can now play around with the constant it wont change the real constant just your local play copy. It's not something to encourage in that usage. The only real valid use of the directive is when you really are dealing with a real cache such as synchronization objects like mutexes &

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups