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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:00:52+00:00 2026-06-11T22:00:52+00:00

This is a miniature version of a code I’m running in production. I have

  • 0

This is a miniature version of a code I’m running in production. I have found that, my real code, behaves differently under gcc and Intel compilers and my best guess is an undefined behavior. Please consider the following sample code:

#include <iostream>

struct baseFunctor{
    virtual double operator()(double x) const = 0;
};

class mathObj{
    const baseFunctor *tmp1, *tmp2;
public:
    void set(const baseFunctor& b1, const baseFunctor& b2){
        tmp1 = &b1;
        tmp2 = &b2;
    }
    double getValue(double x){
        return (tmp1->operator()(x) + tmp2->operator()(x));
    }
};

int main () {
    mathObj obj;

    struct squareFunctor: public baseFunctor {
        double operator()(double x) const { return x*x; }
    };
    struct cubeFunctor: public baseFunctor {
        double operator()(double x) const { return x*x*x; }
    };

    obj.set(squareFunctor(), cubeFunctor());
    std::cout << obj.getValue(10) << std::endl; 
    return 0;
}

Could obj.set(squareFunctor(), cubeFunctor()); invoke undefined behavior?

  • 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-11T22:00:53+00:00Added an answer on June 11, 2026 at 10:00 pm

    Yes it most definitely does, because you are storing pointers to temporary values that are destroyed at the end of the statement, and then using them. Using a destructed object is undefined behaviour.

    You need to create the values separately, and then call set with them:

    cubeFunctor cf;
    squareFunctor sf;
    
    obj.set(sf, cf);
    

    Note that you cannot solve this problem by storing the functors by value (unless you use templates), because that would cause slicing.

    Also as a side note, you can change getValue to do

    return (*tmp1)(x) + (*tmp2)(x);
    

    To make it look a little prettier (and you still get dynamic dispatch).

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

Sidebar

Related Questions

I have this XML : <?xml version=1.0 encoding=utf-8?> <rss version=2.0 xmlns:atom=http://www.w3.org/2005/Atom> <channel> <atom:link href=http://www.montagnaconamore.it/rss/news.xml
This is an example of the code am using. I use a modified version
I have this code : <div class=images_liste_plante_solo> <div class=image_liste_plante> <img alt=Hosta - Eternal Flame
I have a particular file that Java says is empty... Source Code import java.io.File;
This is the code i have written to dynamically generate Controls: private void GenCtrl(string
This should be a simple one: I have an observableArray object called To in
This is my code: <CheckBox android:id=@+id/sprint_checkbox android:layout_width=fill_parent android:layout_height=wrap_content android:text=@string/sprint_game /> <CheckBox android:id=@+id/marathon_checkbox android:layout_width=fill_parent android:layout_height=wrap_content
This code below allows me to find the word error in all my files
This is my sample code: SPWeb web = SPContext.Current.Web SPList list = web.Lists[TestList]; try
This code won't compile because there is an illegal reference to a static field.

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.