Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8890395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:29:32+00:00 2026-06-14T22:29:32+00:00

If I have a template that wraps a standard container, it seems I can

  • 0

If I have a template that wraps a standard container, it seems I can reasonably easily delegate the initializer_list constructor:

template<typename T>
struct holder {
    T t_;

    holder() :
        t_() {}

    holder(std::initializer_list<typename T::value_type> values)
        : t_(values) {}

};

So this works nicely with std::vector, for instance.

int main(int argc, char* argv[]) {

    holder<std::vector<int>> y{1,2,3};
    return EXIT_SUCCESS;
}

But it pretty obviously doesn’t work for T as ‘int’, or any other type that doesn’t have a nested value_type typedef. So, I’d like to use some sort of enable_if or similar trick to make the initializer_list constructor not be emitted unless T both defines a nested value_type typedef, and is constructible from std::initializer_list.

I tried the following, but it still doesn’t work, because the compiler (clang++ 3.1 in my case), still trips over the invalid T::value_type when T is int:

holder(typename std::enable_if<std::is_constructible<T, std::initializer_list<typename T::value_type>>::value, std::initializer_list<typename T::value_type>>::type values)
    : t_(values) {}

Any thoughts on how to express the concept “give this template on T an initializer list constructor over T’s value_type, if and only if T has a value_type typedef and is constructible from an initializer_list of T::value_type”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:29:33+00:00Added an answer on June 14, 2026 at 10:29 pm

    SFINAE only works on template parameter subsitution(hence the S in SFINAE). The following works:

    template<typename T>
    struct holder {
        T t_;
    
        holder() :
            t_() {}
    
        template<typename U = T>
        holder(typename std::enable_if<std::is_constructible<U, std::initializer_list<typename U::value_type>>::value, 
        std::initializer_list<typename U::value_type>>::type values)
        : t_(values) {}
    
    };
    

    If you didn’t use a template function, then the whole class would be instantiated for the type int(in your example), leading to a compiler error.

    Note that you could make the function signature nicer if you used an extra template parameter:

    template<typename U = T, class = typename std::enable_if<std::is_constructible<U, std::initializer_list<typename U::value_type>>::value, bool>::type>
        holder(std::initializer_list<typename U::value_type> values)
        : t_(values) {}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using MSVC2010... I have a struct that wraps a std::string, with standard move ctor's
I have a custom iterator template class that wraps up a std::list iterator. In
Suppose you have the following code template<typename T,void (T::*m)(int)> struct B{ void f(T* a,int
I have template that looks like this: 100 template<size_t A0, size_t A1, size_t A2,
I have a template that displays the titles of a custom post type. I
I have a template that creates a unique identifier for each type it is
I have a ListView template that's being used in several places and I'd like
I have a Meteor template that includes the following: {{#with selected_recipe}} {{>recipe}} {{/with}} In
I have a Django template that I want to extend in multiple places. In
I have Microsoft Word template that I automated filling it's fields from my application,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.