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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:06:29+00:00 2026-05-30T02:06:29+00:00

I need to obtain the type which was supplied when instantiating a template. Consider

  • 0

I need to obtain the type which was supplied when instantiating a template. Consider the following example:

template <typename T> struct Foo
{
  typedef T TUnderlying;
};

static Foo<int> FooInt;

class Bar
{
public:
  auto Automatic() -> decltype(FooInt)::TUnderlying
  {
    return decltype(FooInt)::TUnderlying();
  }
};

int main()
{
  Bar bar;
  auto v = bar.Automatic();
    return 0;
}

Problem with this code is using the scope operator together with decltype. Visual C++ 2010 complains like this:

error C2039: ‘TUnderlying’ : is not a member of ‘`global namespace”

I gathered some information on the topic on Wikipedia:

While commenting on the formal Committee Draft for C++0x, the Japanese ISO member body noted that “a scope operator(::) cannot be applied to decltype, but it should be. It would be useful in the case to obtain member type(nested-type) from an instance as follows”:[16]

vector<int> v;
decltype(v)::value_type i = 0; // int i = 0;

This, and similar issues were addressed by David Vandevoorde, and voted into the working paper in March 2010.

So I reckon the Visual C++ 2010 does not have this implemented. I came up with this workaround:

template <typename T> struct ScopeOperatorWorkaroundWrapper
{
  typedef typename T::TUnderlying TTypedeffedUnderlying;
};

auto Automatic() -> ScopeOperatorWorkaroundWrapper<decltype(FooInt)>::TTypedeffedUnderlying
{
  return ScopeOperatorWorkaroundWrapper<decltype(FooInt)>::TTypedeffedUnderlying();
}

Did I miss any solution which is more elegant and less verbose?

  • 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-30T02:06:29+00:00Added an answer on May 30, 2026 at 2:06 am

    This transparently replaces the decltype keyword with the template based workaround. Once you no longer need to support MSVC2010 you can remove the macro definition without changing any user code:

    #if _MSC_VER == 1600
    #include <utility>
    #define decltype(...) \
      std::identity<decltype(__VA_ARGS__)>::type
    #endif
    

    Which allows this to compile and work on MSVC10:

    std::vector<int> v;
    decltype(v)::value_type i = 0;
    

    Note that std::identity isn’t part of the C++ standard, but it’s safe to rely on it here as the workaround is limited to a compiler which includes std::identity in its standard library implementation.

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

Sidebar

Related Questions

basically I have string s = Foo I need to obtain Type t =
I need to obtain the number of parameters a given block takes. For example:
I have written a small java application for which I need to obtain performance
I need to obtain delivery tracking details from the Canada Post website, which does
Need to locate the following pattern: The letter I followed by a space then
I'm developing some algorithms in OCaml which need some parts to be pluggable so
I need to obtain all the posts that have the same tag. But I
How can you obtain the Type (the name as a string is sufficient) of
I need to use a string as the ID to obtain some object. At
In a remote site, there is a HTML file (say http://www.example.com/abc.html ), which reads:

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.