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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:44:11+00:00 2026-06-13T07:44:11+00:00

I’ve got a question regarding variadic templates. I’ve got a class that uses them

  • 0

I’ve got a question regarding variadic templates. I’ve got a class that uses them in the following way:

template <class... T>
struct A {
  A(B& arg);                   // (1)
  A(typename T::B& args...);   // (2)
};

typename T::B is some type that is expected to be equal for all instances in the parameter pack. For ease of presentation, I refered to this type as B. The class contains an instance of B for each parameter in the parameter pack. The second constructor (2) initializes these members. For convenience, there is a constructor (1) that takes just one instance and initializes all members with the same instance.

The definition of the constructors is not really important for my problem, you can leave them empty. A more complete example is provided below.

Now, the problem is that the constructors conflict, if you initialize A with just one parameter. g++-4.7 was a little confused here and bailed out, but after taking a closer look at the class the problem was obvious.

Questions:

  1. What does the standard say about the situation? Is this an ambiguity that should / can be resolved by the compiler or am I supposed to avoid this situation?

  2. What are the best strategies for avoiding it? Not specifying something like the first constructor at all? I could also put the functionality of the first constructor in a static method, but that would make the API more inhomogeneous.

Thanks for your answers!


Full example:

struct B {};

struct C
{
  using B = ::B;
};

template <class... T>
struct A
{
  A(B& arg) {}
  A(typename T::B & ... args) {}
};

int main()
{
  A<C> x(B()); // Edit: Should be: A<C> X{B()}; But not related to the problem.
  return 0;
}
  • 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-13T07:44:13+00:00Added an answer on June 13, 2026 at 7:44 am
    A<C> x(B());
    

    Is a function declaration, not an object declaration. You need to add parens or use braces:

    A<C> x { B() };
    

    or

    A<C> x((B()));
    

    Also, to pass a temporary B to the constructor by reference, you have to make it const:

    A(const B& arg) { }
    

    Then

    A<C> x((B()));
    

    Works fine.

    Now to address the ambiguity problem, you need something like this:

    #include <type_traits>
    #include <iostream>
    
    struct B {};
    
    struct C
    {
      using B = ::B;
    };
    
    template <class... T>
    struct A
    {
      A(const B& arg) {
        std::cout << "one" << std::endl;
      }
    
      template<bool IsNotOne = sizeof...(T) != 1>
      A(const typename std::enable_if<IsNotOne || !std::is_same<B, T>::value, T>::type::B&... args) {
        std::cout << "multiple" << std::endl;
      }
    };
    
    int main()
    {
      A<B> x1 { B() };         // prints one
      A<C> x2 { B() };         // prints one
      A<C, C> x3 { B(), B() }; // prints multiple
      A<B, B> x4 { B(), B() }; // prints multiple
      return 0;
    }
    

    We make the second constructor a template so we can rely on SFINAE.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this

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.