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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:39:12+00:00 2026-05-28T02:39:12+00:00

The following code compiles in MSVC++, but does not compile in GCC 4.5.1: #include

  • 0

The following code compiles in MSVC++, but does not compile in GCC 4.5.1:

#include <iostream>

template< typename PT, bool pB >
struct TA
{
  PT m;
  TA( PT fT ) :
    m( fT )
  {
   std::cout << "TA<" << pB << ">::TA() : " << m << std::endl;
  }
  PT operator()( PT fT )
  {
   std::cout << "TA<" << pB << ">::() : " << m << " and " << fT << std::endl;
   return ( m );
  }
};

template< typename PT >
PT Foo( PT fT )
{
 std::cout << "Foo() : " << fT << std::endl;
 return ( fT );
}

// Does not compile in GCC 4.5.1, compiles in MSVC++2010. 
// Substitute TA< decltype( fF( std::forward<PP>( fP ) ) ), pB > with
// TA< double, pB > to compile with GCC.
template< bool pB, typename PF, typename PP >
auto Func( PF fF, PP && fP, TA< decltype( fF( std::forward<PP>( fP ) ) ), pB > && fA )
-> decltype( fF( std::forward<PP>( fP ) ) )
{
 decltype( fF( std::forward<PP>( fP ) ) ) lResult( fF( std::forward< PP >( fP ) ) );

 fA( lResult );

 return ( lResult );
}

int main( void )
{
 Func< true >( Foo< double >, -1.2, 2.1 );
 return ( 0 ); 
}

The comment points to the problematic line and shows the fix (which is not really a fix from design point of view, just a compilation fix). Few questions:

  1. Is MSVC++ correct to compile this?

  2. If we were to change parameters’ order in

auto Func( PF fF, PP && fP, TA< decltype( fF( std::forward<PP>( fP ) ) ), pB > && fA )

to

auto Func( PF fF, TA< decltype( fF( std::forward<PP>( fP ) ) ), pB > && fA, PP && fP )

it would not compile, since compiler treats fP in TA< decltype( fF( std::forward<PP>( fP ) ) ), pB > as undeclared variable. Logically, does compiler really need to know fP at this point, does it not parse the whole declaration anyway, since it’s function with trailing return-type? Why can it not “skip” second function parameter and see if fP is declared in function declaration later? Or I’m missing something fundamental here (perhaps, some paragraph from the Standard)?

  • 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-28T02:39:12+00:00Added an answer on May 28, 2026 at 2:39 am
    1. I believe MSVC is correct.

    2. You cannot refer to a later parameter in the declaration of an earlier one, because the name is not yet in scope. In order to determine the type of your fA parameter, the both fP and fF must be in scope so that the decltype expression can be evaluated.

    If you explicitly specify the template parameters then it works with gcc:

    Func< true,decltype(&Foo<double>),double>( Foo< double >, -1.2, 2.1 );
    

    That and the fact that specifying “double” directly as the parameter for TA works implies that the use of a dependent type for the TA parameter is preventing the type deduction for PF and PP. I can’t see why that would be though.

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

Sidebar

Related Questions

The following code builds error free on msvc, but does not compile on g++.
The following code compiles fine, but does not allow the user to choose whether
The following code, compiles fine MSVC, but when building GCC provides a lot of
The following code compiles in Visual C++ and gcc, but fails with Code Warrior
I have the following code that compiles and works well: template<typename T> T GetGlobal(const
The following code compiles perfectly with Eclipse, but fails to compile with javac: public
The following code compiles and runs perfectly, #include <iostream> class sam { public: void
The following code compiles fine using MSVC 2008. When you build GCC climbs a
The following code compiles fine in VC6 but when I compile the same project
The following code compiles and runs but I'm not sure what exactly is going

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.