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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:41:41+00:00 2026-06-13T01:41:41+00:00

I got the errors compiling this code with g++ 4.6 and 4.8. g++ 4.2

  • 0

I got the errors compiling this code with g++ 4.6 and 4.8.
g++ 4.2 and 4.4 is OK. Is it a bug or some new language feature?

template <typename T>
struct A { typedef typename T::value_type type; };

template <typename U>
struct B
{
  void bar () { }
  void foo ()
  {
    // OK
    this->bar ();

    // OK
    (*this).bar ();

    // Error in g++ 4.6-4.8 
    // leads to full instantiating of template arg "U"
    (&*this)->bar ();
  }
};

int main ()
{
  B< A<void> > b;
  b.foo ();
  return 0;
}

g++ inst.cc

inst.cc: In instantiation of ‘struct A<void>’:
inst.cc:20:5:   required from ‘void B<U>::foo() [with U = A<void>]’
inst.cc:27:10:   required from here
inst.cc:3:34: error: ‘void’ is not a class, struct, or union type
   typedef typename T::value_type type;
                                  ^

Update 1: A cannot be instantiated, I know.

The question is: why the compiler tries to instantiate it at “(&*this)->bar ()” line, but not at “this->bar ()” or “(*this).bar ()” lines?

Update 2:

The suggested workaround with addressof (object) is not working for me, because actually I got the error when I tried to use std::bind (&B::bar, this). The real code is much more complex of course and the bind was not used standalone, but the problem was traced to the simple std::bind expression.

I did not want to rewrite or reinvent std::bind, so I had to use CRTP to make it work:

#include <tr1/functional>
template <typename T>
struct A { typedef typename T::value_type type; };

template <typename Derived, typename U>
struct B
{
  Derived* derived (void) { return static_cast<Derived*>(this); }

  void bar () { }
  void foo ()
  {
    // error with recent compiler.
    // std::tr1::bind (&B::bar, this) ();

    // now ok
    std::tr1::bind (&Derived::bar, derived ()) ();
  }
};

struct C: B<C, A<void> >
{
};

int main ()
{
  C c;
  c.foo ();
  return 0;
}

I find such errors and workarounds to be completely illogical though.

  • 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-13T01:41:42+00:00Added an answer on June 13, 2026 at 1:41 am

    Analysis/explanation:

    What you are seeing is shallow instantiation, not full (see below for proof).

    ADL is the culprit here.

    Hypothesis II I’m suspecting an ADL-related thing here (classes can have static free functions (friends) declared inline. Perhaps the compiler needs to instantiate the whole class template in order to make sure it has seen the operator overloads declared in it (in order to do overload resolution).

    The standard backs me up here: §3.4.2 (p46 in n3337):

    ² [snip] The sets of namespaces and classes is determined entirely by
    the types of the function arguments (and the namespace of any template
    template argument). [snip] The sets of namespaces and classes are
    determined in the following way:

    • [snip]

    • If T is a class type (including unions), its associated classes are: the
      class itself; the class of which it is a member, if any; and its direct and
      indirect base classes. Its associated namespaces are the namespaces of which
      its associated classes are members. Furthermore, if T is a class template
      specialization, its associated namespaces and classes also include: the
      namespaces and classes associated with the types of the template
      arguments provided for template type parameters
      (excluding template
      template parameters); the namespaces of which any template template arguments
      are members; and the classes of which any member templates used as template
      template arguments are members.

    The bolded phrase includes class A<void> as a lookup namespace for ADL.

    Workaround:

    In your situation std::addressof(b) can be used instead of &b and it will work.

    Demonstration:

    See http://liveworkspace.org/code/4f85a06598eebe1d8060112be36f4a29

    Note: the (unqualified-id) trick is defined in §3.4.2 of the standard)

    #include <vector>
    #include <iostream>
    
    struct Base {};
    
    template <typename U> struct B : Base { };
    
    template <typename T> struct A {
        typedef typename T::value_type type;
        friend void freefunction(B<A>&) { std::cout << "ADL was here!\n"; }
    };
    
    void freefunction(Base& /*acceptAll*/) {}
    
    int main ()
    {
        B< A<std::vector<int> > >  a;
        B< A<void> >               b;
    
        // surrounding with parens prevents ADL:
        (freefunction)(a);
        (freefunction)(b); // selects ::freefunction(Base&)
    
        freefunction(a);   // ADL selects friend inline freefunction(B< A<std::vector<int> > >&)
      //freefunction(b);   // ADL fails: template arg cannot be (shallow) instantiated
    }
    

    Prints

    ADL was here!
    

    Also, you can verify that the template argument (A<void>) gets shallow instantiated only. Moving the ill-formed typedef into a member function removes the problem:

    template <typename T> struct A {
        void uninstantiated() {
            typedef typename T::value_type type;
        }
        friend void freefunction(B<A>&) { std::cout << "ADL was here!\n"; }
    };
    

    Outputs (http://liveworkspace.org/code/a15c933293281d0926e8b1ff39180079)

    ADL was here!
    ADL was here!
    

    History:

    1. I noticed operator& was the problem, but std::addressof() was ok!
    2. I noticed use of any (overloaded) operators seems to trigger this behaviour

    This lead me to my ‘Hypothesis II’ (see above)

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

Sidebar

Related Questions

I got some quite strange errors compiling code under gcc. It tells me that
I'm having some trouble understanding why this code won't work. I got it from
Thanks for reading this. When compiling got this error: 'ASP.Helpers.Message.DisplayMessage(string, string)': not all code
I need help... I got errors with my android xml... Here's the code: <?xml
I got no errors in the code now, but it doesn't seem to work.
I have got many errors while installing shotdetect software. some errors were because of
After compiling my java code under Eclipse, I got the following error messages: SLF4J:
Actually I've a problem with compiling some library with intel compiler. This same library
Im trying to use this kind of iterators, but I have got errors for
When I compile the following code, I get errors. I don't understand some of

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.