Thread: Hi, Curly brackets in a function call? What's that all about :)

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    209

    Hi, Curly brackets in a function call? What's that all about :)

    I'm feeling a bit dodgy tonight so I've resolved the only sensible thing to do is press on with learning code lol! So I'm asking the question stated in the title. Check this code out:

    Code:
    void SetWindow(CoreWindow const& window)
    {
    .....some time later

    Code:
        window.Activated({ this, &App::OnWindowActivationChanged });
    
        window.SizeChanged({ this, &App::OnWindowSizeChanged });
    
        window.Closed({ this, &App::OnWindowClosed });
    
    };
    All those function calls (which is what I think they are or are they data members?) are having standard brackets with these curly brackets inside them. I'm amazed this doesn't throw the compiler.

    Is this code like making structs on the fly and passing them to function calls or something? It's totally new to me to see this, thanks

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,644
    Is this code like making structs on the fly and passing them to function calls or something?
    Yes, the calls are initializing and passing temporary instances of a struct.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
     
    struct A {
        int n;
        string s;
    };
     
    void f(A a) {
        cout << a.n << ' ' << a.s << '\n';
    }
     
    int main() {
        A x {1, "hi"};
        f(x);
        f({2, "bye"});
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    List-initialization (since C++11) - cppreference.com

    Look at how the member function window.Activated is declared, and what you're seeing is a list initialiser for that parameter type.

    There has been a LOT of additions to C++ in the last decade.
    If you've got an old book, new code and a new compiler, then you're likely to find a lot of weird looking stuff.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,644
    Maybe I don't know what I'm talking about here.
    Is this even C++?
    Can you give a link to the code?
    @Salem, can you give a link to "how the member function window.Activated is declared"? All I can find is C#.
    It's hard to see why it would be list-initialization in a case that apparently needs exactly two things of specific types.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    209
    Hello chaps!

    Thanks much for the replies really, really good cheers!

    The code is taken from here it's the microsoft sorta tutorial on making a simple game in visual studio using DirectX12, afaik it is C++ as it states that in the lesson prior to this one.

    Define the game's UWP app framework - UWP applications | Microsoft Docs

    Thanks

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    209
    Hey thanks very much, I chose C++/WinRT - there have it seems been many changes in recent years. I learned C++ a long time ago before all these new things came out. I'm amazed at how much there still is to learn about something I "thought" I knew lol

    Note to self - learn what a list initialiser is

    Mucho thanks for the replies (Salem & John).
    Last edited by shrink_tubing; 05-30-2022 at 07:45 AM.

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    209
    Well then what a topic this has been. I did some research and here's what I found.

    Firstly I went and researched what a list initialiser is, and it sure broadened my knowledge. The examples I worked through were with structs and using constructors for structs in the definition. I didn't even know you could do that lol! I got to see where a default constructor and a member initialiser could both be in a struct and depending on how the struct was created either one or the other would run.

    Was really cool, even fun!

    I also opened up Code::Blocks and started poking around some Microsoft files for windows. I prefer CB's search facility I can never get on with the one in VS 2015 (or just haven't learned to use it properly yet.

    Well anyways....eventually I found this:

    Code:
    ITypedEventHandler<ABI::Windows::UI::Core::CoreWindow*, ABI::Windows::UI::Core::WindowActivatedEventArgs*> : ITypedEventHandler_impl<ABI::Windows::Foundation::Internal::AggregateType<ABI::Windows::UI::Core::CoreWindow*, ABI::Windows::UI::Core::ICoreWindow*>, ABI::Windows::Foundation::Internal::AggregateType<ABI::Windows::UI::Core::WindowActivatedEventArgs*, ABI::Windows::UI::Core::IWindowActivatedEventArgs*>>
    Now to be clear this is referenced many times in the file and I mostly have no idea what it's doing (this is rather beyond my coding ken at this time) but it did at least appear as though in a very sophisticated way the function expects two arguments. One seems to be a pointer to the window and the other a pointer to the event arguments. It appears these are also almost defined in a way in the same line but that's a bit too much for me to understand at this time.

    I thought you'd appreciate the feedback. For me however I'm in over my head now as that's a bit too much for me for to understand beyond only the gist of it. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-22-2016, 09:08 AM
  2. Replies: 1
    Last Post: 03-20-2016, 08:58 PM
  3. IF Statement-- With curly brackets or semicolon
    By erdemtuna in forum C Programming
    Replies: 6
    Last Post: 01-10-2016, 06:40 AM
  4. Function call Overhead and Function Call Stack
    By Alam Khan in forum C++ Programming
    Replies: 2
    Last Post: 04-26-2014, 08:28 AM
  5. is curly braces here is a must?
    By mashour06 in forum C Programming
    Replies: 8
    Last Post: 04-25-2009, 04:41 PM

Tags for this Thread