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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:52:35+00:00 2026-06-09T16:52:35+00:00

I am trying to write a function that will set the first argument to

  • 0

I am trying to write a function that will set the first argument to the value of the second argument. However, when the second argument is a container class, I want it to set the first argument to the first element of the container. I found this question which answers a similar problem, however, I can not seem to get it to work in my case.

When I compile this code I get an error saying that SetVar is ambiguous. Is there anyway to get the functionality working?

Here is my code so far…

#include <iostream>
#include <vector>
template<typename T1,typename T2>
static void SetVar(T1& a, const T2 &b, typename  T2::const_iterator= T2().begin()){
    //Check to make sure b contains an element.
    if(b.begin()!=b.end())  a=*b.begin();
}
template<typename T1,typename T2>
static void SetVar(T1& a, const T2 &b,...){
    a=b;
}
int main(int argc, const char * argv[])
{
    int x;
    int y=5;
    std::vector<int> z;
    z.push_back(1);
    z.push_back(3);
    SetVar(x, y);
    //Should print 5
    std::cout<<x<<"\n";

    SetVar(x, z);//<---SetVar is ambiguous
    //Should print 1
    std::cout<<x<<"\n";
    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-09T16:52:36+00:00Added an answer on June 9, 2026 at 4:52 pm

    Passing argument to an ellipsis does make a function overload less preferred than one that has an actual parameter for the same argument, but that doesn’t come up here because there is no third argument in your calls.

    I would use enable_if for both:

    #include <type_traits>
    
    // Enabled if T2 has `const_iterator` and `begin()`:
    template<typename T1,typename T2>
    auto SetVar(T1& a, const T2 &b)
      -> typename std::enable_if<std::is_convertible<
             decltype(std::declval<const T2&>().begin()),
             typename T2::const_iterator>::value
         >::type
    {
        //Check to make sure b contains an element.
        if(b.begin()!=b.end())  a=*b.begin();
    }
    
    // Enabled if expression 'a=b' is valid.
    template<typename T1,typename T2>
    auto SetVar(T1& a, const T2 &b)
      -> typename std::enable_if<std::is_assignable<T1, const T2&>::value>::type
    {
        a=b;
    }
    

    It’s still possible for the above to be ambiguous, but only if both conditions are true, which means a weird container or implicit conversion is going on – and in that case I would want the compiler to warn me of the confusion.

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

Sidebar

Related Questions

I am trying to write a function that will allow me to browse for
I'm trying to write a function that will: If there is no region selected,
i am trying to write a function that will make DataRow[column] return nullable typed
I'm trying to write a function that will let me red-shift or blue-shift a
I am trying to write a function that will filter a list of tuples
I am trying to write a function that will: Disable the submit button when
I am trying to write a function that will randomly return an (x,y) co-ordinates
I'm trying to write a function that will return all the available cars for
I am trying to write a function that will convert a generic list to
I'm trying to write a short function that will let me quickly read in

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.