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

  • Home
  • SEARCH
  • 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 588513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:19:34+00:00 2026-05-13T15:19:34+00:00

How come I can’t instantiate an object of type Foo with above constructor? I

  • 0

How come I can’t instantiate an object of type Foo with above constructor?

I have a class Bar that uses an internal typedef (as a workaround for “template typedefs”) and intend to use it in a constructor as below (CASE 1).
However, I don’t seem to get it to compile. Is this legal C++?
CASE 2 seems to suggest the problem is related to the typedef in Bar.

How can I define a constructor that will accept std::vectors of objects with the type in Bar?

#include <vector>
#include <iostream>
#include <utility>

template <typename T>
struct Bar
{
    typedef std::pair<T, T> type; // or anything else that uses T
};

struct Foo
{
    Foo() {}

    // CASE 1: doesn't compile
    template <typename T> explicit Foo( const std::vector<typename Bar<T>::type>& data )
    {
        std::cout << "Hello\n";
    }

    //// CASE 2: compiles, but it's not what I want
    //template <typename T> explicit Foo( const std::vector<Bar<T> >& data )
    //{
    //  std::cout << "Hello\n";
    //}
};

int main()
{
    std::vector<Bar<int>::type> v; // for CASE 1
    //std::vector<Bar<int> > v; // for CASE 2

    Foo f( v );
    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-05-13T15:19:34+00:00Added an answer on May 13, 2026 at 3:19 pm

    According to paragraph 14.8.2.1 of the C++ standard, when a template parameter is used only in a non-deduced context, the corresponding template argument cannot be deduced:

    If a template-parameter is not used in any of the function parameters of a function template, or is used only in a non-deduced context, its corresponding template-argument cannot be deduced from a function call and the template-argument must be explicitly specified.

    The definition of nondeduced contexts, as stated in §14.8.2.4:

    The nondeduced contexts are:

    • the nested-name-specifier of a type that was specified using a qualified-id.

    • A type that is a template-id in wich one or more of the template-arguments is an expression that references a template-parameter.

    In Bar<T>::type, Bar<T> is a nested-name-specifier and hence a non-deduced context, so you must explicitly specify the template argument when calling the constructor…which is not possible (i.e. you cannot write Foo f<int>(v)).

    I suppose the compiler cannot deduce the template argument because that would be at least cumbersome, and more probably impossible: imagine Bar is specialized:

    template<typename T>
    struct Bar
    {
        typedef std::pair<T,T> type;
    };
    
    template<>
    struct Bar<char>
    {
        typedef std::pair<int,int> type;
    };
    

    Now I have an ambiguity when calling Foo’s constructor with std::vector<std::pair<int,int> >: should the template argument be int or char? And even if there was no such ambiguity, you can easily see that the compiler would have to instantiate Bar with potentially any type before finding the instantiation with the correct typedef (well, I’m not so sure above statements are truly relevant since I often find out compilers to be much more smarter than I thought :-)!)

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

Sidebar

Related Questions

I have a simple jQuery question. How come I can't do this? $(<div class='flora'></div>).insertBefore('</body>');
I am wondering how come I can reach inner class directly from outer type
I'm reading lines of text that can come in any order. The problem is
Folks, we all know that IP blacklisting doesn't work - spammers can come in
I have a variable containing a string. The string can come in several ways:
Does anyone know where I can come by a regex that matches any valid
I'm new to nodejs, the only way that I can come up with is
In my c++ class, we got assigned pairs. Normally I can come up with
How come you can't use the Class's traversing methods with variable's to set the
This was my attempt. The best that I can come up with is getting

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.