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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:35:55+00:00 2026-06-03T09:35:55+00:00

I have a strange metafunction behavior in my C++ code and I want to

  • 0

I have a strange metafunction behavior in my C++ code and I want to understand why.

#include <iostream>
#include <cmath>

inline double f(double x, double y)
{
    std::cout<<"Marker"<<std::endl;
    return sqrt(x*y);
}

template <int N, class T> inline T metaPow(T x)
{
    return ((N > 0) ? (x*metaPow<((N > 0) ? (N-1) : (0))>(x)) : (1.));
}


int main()
{
    double x;
    double y;
    std::cin>>x;
    std::cin>>y;
    std::cout<<metaPow<5>(f(x, y))<<std::endl;
    return 0;
}

I expected that the line metaPow<5>(f(x, y)) was equivalent to f(x, y)*f(x, y)*f(x, y)*f(x, y)*f(x, y)*1.. But if it was, it would print me five times the “Marker” line in the f function.

The strange thing is that I have the good result at the end (for example 181.019 for x = 2 and y = 4) but I have only 1 “Marker” displayed. How is it possible ? And consequently is it a good option to use that function for compile-time optimization instead of the standard pow() ?

Thank you very much !

  • 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-03T09:35:56+00:00Added an answer on June 3, 2026 at 9:35 am

    I believe that f(x,y) is being evaluated before being passed in to your metaPow function. So the x argument to metaPow is just the value sqrt*(8). metaPow is never calling f(x,y). Hence, f(x,y) is only called once – when you initially call metaPow in your main function.

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

Sidebar

Related Questions

I have strange behavior with android.R.layout.simple_dropdown_item_1line while using MultiAutoCompleteTextView . The code I use
I have strange MPMoviePlayerController behavior. It don't want to play movies just chosen from
I have strange discarding behavior of struts2 while setting cache option for my image.
I have strange stacktrace when click to submit button (this code is rendered but
The <button> tag appears to have strange behavior in Safari and Opera (but not
I have strange behavior of Sphinx searchd. I used it with Python standard client
I have a strange problem regarding null pointer exception. I'm posting the problematic code
I have strange problem with XUL layouts. My current code: <xul:vbox> <xul:hbox> .. some
I have strange problem when using at() method of std::string. I'd like to calculate
I have strange behavior in FuelPHP. I generate form field with FuelPHP Form::input() method.

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.