C++0x initializer list example

10,942

You last examples wouldn't be allowed as you ask for pointers but try to provide local temporary objects instead.

std::vector<Ask*> ui ={
    new AskString{"Enter your name: ", 3, 25},
    new AskString{"Enter your city: ", 2, 25},
    new Ask{"Enter your age: "}
    };

That would be allowed and there would be no type ambiguity.

That would be right too :

std::vector<Ask*> ui ={
        new AskString("Enter your name: ", 3, 25),
        new AskString("Enter your city: ", 2, 25),
        new Ask("Enter your age: ")
        };

And your example is more like :

std::vector<Ask> ui ={  // not pointers
    {"Enter your name: "},
    {"Enter your city: "},
    {"Enter your age: "}
    };

std::vector<AskString> uiString ={  // not pointers
    {"Enter your name: ", 3, 25},
    {"Enter your city: ", 2, 25},
    {"Enter your age: ", 7, 42}
    };

and again there would be no ambiguity on the types.

Share:
10,942
CW Holeman II
Author by

CW Holeman II

You may have just discovered a dozen VMS posts today, but up voting them together causes all of the votes to be deleted. C++17 and XSLT: Aptcp C++ and I18N: Ask Library (C++) Perl, Javascript, XUL: Stevedore - DocWorker JavaScript, SVG, DOM, XML and XSLT: Emle - Electronic Mathematics Laboratory Equipment documentation VAX FORTRAN: SDSU_AskLib VMS, VAXELN, RSX-11M/M+, C, DCL, TCL/Tk, Scala OpenHub/cwhii Resume Windows NT looks like MS-Windows with features from VMS but in its heart it is like VAXELN...ZuckOS

Updated on August 02, 2022

Comments

  • CW Holeman II
    CW Holeman II over 1 year

    I would like to see how this example of existing code would be able to take advantage of the C++0x initializer list feature.

    Example0:

    #include <vector>
    #include <string>
    struct Ask {
        std::string prompt;
        Ask(std::string a_prompt):prompt(a_prompt){}
    };
    struct AskString : public Ask{
        int min;
        int max;
        AskString(std::string a_prompt, int a_min, int a_max):
            Ask(a_prompt), min(a_min), max(a_max){}
    };
    int main()
    {
        std::vector<Ask*> ui;
        ui.push_back(new AskString("Enter your name: ", 3, 25));
        ui.push_back(new AskString("Enter your city: ", 2, 25));
        ui.push_back(new Ask("Enter your age: "));
    }
    

    Would it support something like this:

    Example1:

    std::vector<Ask*> ui ={
        AskString("Enter your name: ", 3, 25),
        AskString("Enter your city: ", 2, 25),
        Ask("Enter your age: ")
        };
    

    Or must it have literals like this?:

    Example2:

    std::vector<Ask*> ui ={
        {"Enter your name: ", 3, 25},
        {"Enter your city: ", 2, 25},
        {"Enter your age: "}
        };
    

    If so how would the difference between AskString and Ask be handled?

  • mmmmmmmm
    mmmmmmmm almost 15 years
    If you use std::vector<Ask> you could not add AskString!
  • rlbond
    rlbond almost 15 years
    I thought AskString was a function that returned an Ask, silly me.
  • Johannes Schaub - litb
    Johannes Schaub - litb over 13 years
    This is not true. A C++ initializer_list is, but not a initializer list.