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 8951965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:46:13+00:00 2026-06-15T13:46:13+00:00

I had a problem in a highly templated code, and I isolated it in

  • 0

I had a problem in a highly templated code, and I isolated it in this example program (I compile with g++ 4.7.1) :

#include <iostream>
#include <initializer_list>
#include <type_traits>
#define OPTION 2

// Test class
template<typename T = double>
class MyClass
{
    public:
        // Constructors
        MyClass(const MyClass<T>& source) 
        {std::cout<<"copy constructor"<<std::endl;}

        #if OPTION == 1
        template<typename T2 = T> 
        MyClass(const std::initializer_list<T2>& source) 
        {std::cout<<"init constructor"<<std::endl;} // OPTION 1
        #elif OPTION == 2
        template<typename T2 = T, class... Misc> 
        MyClass(const std::initializer_list<T2>& source, const Misc&... misc) 
        {std::cout<<"init+misc constructor"<<std::endl;} // OPTION 2
        #endif

        template<class... Misc> 
        explicit MyClass(const Misc&... misc) 
        {std::cout<<"misc constructor"<<std::endl;}

        // Assignment
        inline MyClass<T>& operator=(const MyClass<T>& rhs) 
        {std::cout<<"copy assignment"<<std::endl; return *this;}

        template<class Misc> 
        inline MyClass<T>& operator=(const Misc& rhs) 
        {std::cout<<"misc assignment"<<std::endl; return *this;}
};

// Main
int main(int argc, char* argv[])
{
    MyClass<double> x;
    x = {4., 8., 15., 16., 23., 42.}; // CALL LINE
    return 0;
}

The problem is the following. With OPTION == 1, all is ok, and the CALL LINE calls the “init constructor”.

But with OPTION == 2, I have the following error message :

error: converting to ‘const MyClass<double>’ from initializer list would use
explicit constructor ‘MyClass<T>::MyClass(const Misc& ...) [with Misc = {double, 
double, double, double, double, double}; T = double]’

The question is : why ? Is it normal or is it a bug of g++ ?

Furthemore, I need a workaround : I have to implement the option 2 constructor without option 1. The most obvious thing for me would be to block the call to the explicit constructor with an enable if, like :

    template<class... Misc, class std::enable_if<SOMETHING>::type> 
    explicit MyClass(const Misc&... misc) 
    {std::cout<<"misc constructor"<<std::endl;}

My problem is : what can I write instead of SOMETHING in order to block every std::initializer_list<T2> (for all T2 types) ?

  • 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-15T13:46:14+00:00Added an answer on June 15, 2026 at 1:46 pm

    To the first question, about the error message:

    The constructor in OPTION==2 is not considered as initializer-list constructor, because such a constructor is required to either have a single std::initializer_list<E> parameter (or such a reference) or all other parameters shall have default arguments. (8.5.4 List-initialization [dcl.init.list] #2).

    Therefore, not finding an initializer-list constructor the compiler tries other constructors with argument list consisting of the elements of the initializer list, finds the explicit constructor and, since we’re in the context of implicit conversion (initializing the parameter of the assignment operator), fails with the error message.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had this problem before and can't for life of me remember how to
I had no problem at all running the following code on a local server,
I had this problem some time ago and I gave up but lately it
I had problem while executing this SQL statement in MySQL workbench. Can anyone point
I had problem with this codes: cell_0_value= dgv.CurrentRow.Cells[0].Value.ToString(); It says null at dgv.CurrentRow.Cells[0].Value but
I have a problem with This code in C# : main class { var
I had problem in my other code and huge, but I had made one
I had problem even in choosing a title for this question. Please feel free
I already had this problem multiple times, that I couldn't find a good solution
For standard queries I have not had a problem with this. I simply: ResultSet

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.