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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:59:59+00:00 2026-05-15T01:59:59+00:00

How do i pull off something that would make the last commented line compile?

  • 0

How do i pull off something that would make the last commented line compile? How would i have to change this code to make it work?

#include<iostream>

using namespace std;

template <int x>
class someclass{
 public:
  int size;
  int intarr[x];
  someclass():size(x){}
};

template<int x, int y>
int somefunc(someclass<x> A, someclass<y> B){
 return ( A.size > B.size ? A.size : B.size); 
}

template<int x, int y, int z>
someclass<x> anotherfunc(someclass<y> A, someclass<z> B){
 return ( A.size > B.size ? A : B); 
}

int main(){

 someclass<5> A;
 someclass<10> B;
 cout << "SIZE = " << somefunc(A,B) << endl;
 //cout << "SIZE = " << (anotherfunc(A,B)).size << endl; //THIS DOES NOT COMPILE
 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-15T02:00:00+00:00Added an answer on May 15, 2026 at 2:00 am

    How is it supposed to know what x is from outside the function? (It can’t look into the body, because in which body of which function it looks depends on what value x gets!). Also you cannot write that ?: because both your branches yield totally unrelated types that can’t possibly get to a common type. But that operator requires to have a common type for both branches that it evaluates to.

    And then the other problem you have that size is not a compile time constant is already elaborated by another post.

    I think this should work:

    template<int y, int z>
    someclass<sizeof (char[+(y >= z)]) * y> anotherfunc(someclass<y> A, someclass<z> B){
     return A; 
    }
    
    template<int y, int z>
    someclass<sizeof (char[+(y < z)]) * z> anotherfunc(someclass<y> A, someclass<z> B){
     return B; 
    }
    

    Now when calling it, y and z are deduced by the parameters, and then the parameters are substituted into the return type. The sizeof(char[1 or 0]) will test whether y or z is greater. In the respective template that evaluates this to sizeof(char[1]) (which yields 1) we will multiply by the value that is greater (or equal). If the respective template yields sizeof(char[0]) this is a deduction failure (an array can’t be of zero size!) and the template won’t get selected by overload resolution (known as SFINAE).

    The unary + yields 1 or 0 as int instead of true or false, which could cause compiler warnings for some compilers.


    There is also the “clean” way with enable_if. It’s not doing hideous sizeof tricks, but rather using the general established enable_if pattern. Boost has an implementation of it

    template<int y, int z>
    typename boost::enable_if_c<(y >= z), someclass<y> >::type 
    anotherfunc(someclass<y> A, someclass<z> B){
     return A; 
    }
    
    template<int y, int z>
    typename boost::enable_if_c<(y < z), someclass<z> >::type 
    anotherfunc(someclass<y> A, someclass<z> B){
     return B; 
    }
    

    This may at first sight look more noisy, but should be easier to understand and faster to follow to folks that are already used to enable_if (the _c variant of boost’s implementation accepts plain booleans, as above).

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

Sidebar

Related Questions

I have a list of topic categories that looks something like this mockup: http://www.problemio.com/problems/categories.php
I submitted a pull request with this code: my_sum = sum([x for x in
I have a class that manages keyboard input and fires off KeyPressed , KeyReleased
I have a Javascript array of 20 RGB color values that looks like this:
My Solution/Project builds fine with code contracts turned off for this particular project. When
I have an object of type System.Drawing.Image and would like to display this image
I'm about to pull out any remaining hair that I have, so please help
Just trying to pull off some SMART info from connected Hard Drives on any
I want to search for images using a Python script and pull them off
I'm creating a script to pull a video off the web, create an image

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.