Thread: C++ "function" keyword

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question C++ "function" keyword

    Hi! I discovered that my IDE (CodeBlocks) marked the word "function" in green when I typed it. Discovered that by mistake because I've been programming in PHP for a while.

    But which header file is the keyword "function" in? And how do I use it? I'm just curious. I've searched around on Google for it. But it just shows me how to define a function and things that I already know.

    Thanks for answers!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    std::function is the declared in the standard header <functional>, as of C++11. It's a general-purpose function wrapper.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    What grumpy says is true, but just to be clear though, "function" is not actually a keyword in C++, as suggested in your post title.
    If it were, then you couldn't e.g. declare a variable called "function".
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question

    So it is declared in the header <functional> and isn't a C++ keyword. But what is it used for?
    A code example would be great!

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Did you even try a search? Found this searching for "std::function" -> std::function - cppreference.com

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    function is not a keyword in C++. It is a templated class type that is defined in the standard library.

    As I said in a previous post, it is a function wrapper. So it is (um) used to wrap callable objects (objects that behave like functions).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-27-2008, 11:44 PM
  2. A simple question about "virtual" keyword
    By meili100 in forum C++ Programming
    Replies: 2
    Last Post: 03-07-2008, 01:05 PM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM
  5. Equivalent of Java's "super" keyword
    By JasonLikesJava in forum C++ Programming
    Replies: 6
    Last Post: 05-31-2002, 10:21 AM

Tags for this Thread