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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:52:19+00:00 2026-05-14T04:52:19+00:00

Here is an (artificial) example of using a function that returns an anonymous struct

  • 0

Here is an (artificial) example of using a function that returns an anonymous struct and does “something” useful:

#include <iostream>

template<typename T>
T* func(T* t, float a, float b) {
    if(!t) {
        t = new T;
        t->a = a;
        t->b = b;
    } else {
        t->a += a;
        t->b += b;
    }
    return t;
}

struct {
    float a, b;
}* foo(float a, float b) {
    if(a==0) return 0;
    return func(foo(a-1,b), a, b);
}

int main() {
    std::cout << foo(5,6)->a << std::endl;
    std::cout << foo(5,6)->b << std::endl;

    void* v = (void*)(foo(5,6));
    //[1] delete f now because I know struct is floats only.
    float* f = (float*)(v);

    std::cout << f[0] << std::endl;
    std::cout << f[1] << std::endl;

    delete[] f;

    return 0;
}

There are a few points I would like to discuss:

  1. As is apparent, this code leaks, is there anyway I can NOT leak without knowing what the underlying struct definition is? see Comment [1].
  2. I have to return a pointer to an anonymous struct so I can create an instance of the object within the templatized function func, can I do something similar without returning a pointer?
  3. I guess the most important, is there ANY (real-world) use for this at all? As the example given above leaks and is admittedly contrived.

By the way, what the function foo(a,b) does is, to return a struct containing two numbers, the sum of all numbers from 1 to a and the product of a and b.

Maybe the line new T could use a boost::shared_ptr somehow to avoid leaks, but I haven’t tried that. Would that work?

I think I was just trying to delete the anonymous struct as an array of floats, something like float* f = new float[2]. Which might be wrong, as the comment below suggests, so what can be done? can I delete at all?

I can compile and run this code “as-is” on VS2008, maybe some non-standard extensions might be being used by VS, but it does run and gives 15 and 30 as the answer.

From the answers I believe this contraption is a VS2008 specific entity, it is not standards compliant and thus not portable. Too bad though, I would have liked to see what voodoo the Stackoverflow or Boost people came up with if this was in their arsenal :). Thanks all.

  • 1 1 Answer
  • 1 View
  • 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-14T04:52:20+00:00Added an answer on May 14, 2026 at 4:52 am

    For now, your code is not portable; it will, for example, not build with gcc.

    Section 14.3.1/2 of the standard says:

    A local type, a type with no
    linkage, an unnamed type or a type
    compounded from any of these types
    shall not be used as a template-
    argument
    for a template
    type-parameter.

    See item 488 in the C++ Standard Core Language Defect Reports, Revision 69 and Paper N2657 for one possible evolution.

    UPDATE 1

    Assuming that your code were well-formed, then:

    1. you may be able to rewrite:

      std::cout << foo(5,6)->a << std::endl;
      

      as

      std::cout << std::auto_ptr(foo(5,6))->a << std::endl;
      
    2. you may return an anonymous struct by-value provided that the anonymous struct had a constructor taking another type (anonymous or not, that you’d be able to initialize inside the body of your method) — except, of course, how do you specify a constructor for an anonymous struct? 🙂

    3. no real-world use that I can see other than an extremely convoluted way of trying not to assign an explicit name to the structure; one would typically use anonymous structs (not technically legal in C++, but supported by various compilers as extensions) in order to not pollute the namespace, typically by instantiating one right away (you may for example see one-shot functors being instantiated and passed down as anonymous structs — again, technically not legal C++.)

    UPDATE 2

    Thank you gf for the link to the relevant portion of the C++ standard concerning new types which may not be defined in a return type.

    UPDATE 3

    Bringing this one out here from the comments: calling delete[] on memory allocated with new (as opposed to new[]) is an invitation to heap corruption. Calling delete on a pointer whose type you do not know is technically undefined (which destructor should get called?) but in the case of PODs (your anonymous struct being one) you can get away with it in this horrible hackish way:

     delete (int*)f;
    

    Of course, were your code magically well-formed, std::auto_ptr would have been able to retain the anonymous type and would have taken care of calling delete for you correctly and gracefully.

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

Sidebar

Related Questions

Here is the code in a function I'm trying to revise. This example works
Here is some CRTP based template code that I used to try and resolve
My book here (Artificial intelligence A modern approach) says that the worst-case time and
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
Here is the script I'm using, copied directly from Google: <script type=text/javascript> var _gaq
Small toy app can be found here: http://gist.github.com/517445 I am trying to send artificial
What is convention for local vars that has same meaning as function argument? If
Here's a query that works fine: SELECT rowid as msg_rowid, a, b, c FROM
Here is my simplified data structure: Object1.h template <class T> class Object1 { private:
I need to break from foldl. Here is a dummy example how to break

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.