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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:30:34+00:00 2026-06-14T17:30:34+00:00

Is it implemented already, because this does not compile: (using gcc 4.7.2) template <typename…

  • 0

Is it implemented already, because this does not compile: (using gcc 4.7.2)

template <typename... Ts>
struct Foo {
    int foo() {
        return 0;
    }
};

template <>
struct Foo<int x, int y> {
    int foo() {
        return x * y;
    }
};

int main()
{
    Foo<2, 3> x;
    cout << x.foo() << endl; //should print 6
}
  • 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-14T17:30:36+00:00Added an answer on June 14, 2026 at 5:30 pm

    You are making a few mistakes. The primary template expects types, not
    integral constants. You also try to instantiate the template with
    integral constants, but your partial specialization uses types.

    This is closer:

    #include <iostream>
    
    template <int... Ts>
    struct Foo {
        int foo() {
            return 0;
        }
    };
    
    template <>
    struct Foo<3, 2> {
      const int x = 3;
      const int y = 2;
    
      int foo() {
        return x * y;
      }
    };
    
    int main()
    {
        Foo<2, 3> x;
        std::cout << x.foo() << std::endl; //should print 6
    }
    

    But this is not really what we want, right? And it is also clumsy.

    #include <iostream>
    
    template<typename Acc, typename... Rest>
    struct accum_help; // primary
    
    template<typename Acc, typename F, typename... Rest>
    struct accum_help<Acc, F, Rest...> {
      typedef typename accum_help<
        std::integral_constant<typename Acc::value_type, 
                               Acc::value * F::value>, Rest...
        >::type type;
    };
    
    template<typename Acc>
    struct accum_help<Acc> {
      typedef Acc type;
    };
    
    // peek into the first argument to avoid summing empty sequences and
    // get the right type
    template<typename X, typename... Integrals>
    struct accum {
      typedef typename accum_help<
        std::integral_constant<typename X::value_type, 1>, X, Integrals...
        >::type type;
    };
    
    int main()
    {
    
      std::cout << accum< std::integral_constant<int, 2>, std::integral_constant<int, 3> >::type::value << std::endl; //should print 6
    }
    

    A simpler variant handling only int:

    template <int...>
    struct accum2_help;
    
    template <int Acc, int X, int... Rest> 
    struct accum2_help<Acc, X, Rest...> {
      static const int value = accum2_help< Acc * X, Rest...>::value;
    };
    
    template <int Acc>
    struct accum2_help<Acc> {
      static const int value = Acc;
    };
    
    // again don't accept empty packs
    template <int T, int... Ts>
    struct accum2 {
      static const int value = accum2_help<1, T, Ts...>::value;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I use ValueAwareEditor.onPropertyChange? Is this feature implemented already? It is not being
I've have already implemented pagination using the following code: public Paginacao<Anuncio> consultarPaginado(int pagina, Integer
I've already implemented, using macros, a C++ property system that satisfied the following requirements:
I have already implemented some AJAX pagination in my Rails app by using the
I have already implemented a functionallity, when keeper automatically moves using breadth first search
Need some help with this problem in implementing with XSLT, I had already implemented
Technically this might not be classed as a programming question, since I have already
JPA Controller class method edit() does not check if entity already exist instead adds
I already implemented to create the XML file below with XmlTextWriter when application initialization.
Is there any already implemented function that has initialArray, sizeOfInitialArray, outArray, and sizeOfOutArray parameters,

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.