Using single-member structs as the member itself












0















I use structs to expose a typesafish interface, like:



class IFoo
{
virtual HRESULT bar(struct _BAR * arg) = 0;
}


Behind the scene, each implementation of this interface defines its own struct:



struct _BAR { Baz baz };


I currently call many allocation functions this way:



HRESULT allocate(Baz ** baz);
_BAR * bar;
ret = allocate( (Baz **) &bar );


Is it possible to call them with a more type-safe cast, like static_cast, by adding data in the struct _BAR definition?










share|improve this question


















  • 2





    Heads-up: you have UB here through the pointer cast, but also from the conflicting definitions of _BAR which violate ODR. I think you're knee-deep in a solution which won't pan out.

    – Quentin
    Dec 19 '18 at 14:05






  • 1





    each implementation defines its own struct? _BAR has to be always the same type, and you cannot define the same type more than once

    – user463035818
    Dec 19 '18 at 14:05











  • Alright, I went with a distinct solution. Is this question useful or should I just delete it?

    – bartavelle
    Dec 19 '18 at 15:19
















0















I use structs to expose a typesafish interface, like:



class IFoo
{
virtual HRESULT bar(struct _BAR * arg) = 0;
}


Behind the scene, each implementation of this interface defines its own struct:



struct _BAR { Baz baz };


I currently call many allocation functions this way:



HRESULT allocate(Baz ** baz);
_BAR * bar;
ret = allocate( (Baz **) &bar );


Is it possible to call them with a more type-safe cast, like static_cast, by adding data in the struct _BAR definition?










share|improve this question


















  • 2





    Heads-up: you have UB here through the pointer cast, but also from the conflicting definitions of _BAR which violate ODR. I think you're knee-deep in a solution which won't pan out.

    – Quentin
    Dec 19 '18 at 14:05






  • 1





    each implementation defines its own struct? _BAR has to be always the same type, and you cannot define the same type more than once

    – user463035818
    Dec 19 '18 at 14:05











  • Alright, I went with a distinct solution. Is this question useful or should I just delete it?

    – bartavelle
    Dec 19 '18 at 15:19














0












0








0








I use structs to expose a typesafish interface, like:



class IFoo
{
virtual HRESULT bar(struct _BAR * arg) = 0;
}


Behind the scene, each implementation of this interface defines its own struct:



struct _BAR { Baz baz };


I currently call many allocation functions this way:



HRESULT allocate(Baz ** baz);
_BAR * bar;
ret = allocate( (Baz **) &bar );


Is it possible to call them with a more type-safe cast, like static_cast, by adding data in the struct _BAR definition?










share|improve this question














I use structs to expose a typesafish interface, like:



class IFoo
{
virtual HRESULT bar(struct _BAR * arg) = 0;
}


Behind the scene, each implementation of this interface defines its own struct:



struct _BAR { Baz baz };


I currently call many allocation functions this way:



HRESULT allocate(Baz ** baz);
_BAR * bar;
ret = allocate( (Baz **) &bar );


Is it possible to call them with a more type-safe cast, like static_cast, by adding data in the struct _BAR definition?







c++ visual-c++






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 19 '18 at 13:51









bartavellebartavelle

772614




772614








  • 2





    Heads-up: you have UB here through the pointer cast, but also from the conflicting definitions of _BAR which violate ODR. I think you're knee-deep in a solution which won't pan out.

    – Quentin
    Dec 19 '18 at 14:05






  • 1





    each implementation defines its own struct? _BAR has to be always the same type, and you cannot define the same type more than once

    – user463035818
    Dec 19 '18 at 14:05











  • Alright, I went with a distinct solution. Is this question useful or should I just delete it?

    – bartavelle
    Dec 19 '18 at 15:19














  • 2





    Heads-up: you have UB here through the pointer cast, but also from the conflicting definitions of _BAR which violate ODR. I think you're knee-deep in a solution which won't pan out.

    – Quentin
    Dec 19 '18 at 14:05






  • 1





    each implementation defines its own struct? _BAR has to be always the same type, and you cannot define the same type more than once

    – user463035818
    Dec 19 '18 at 14:05











  • Alright, I went with a distinct solution. Is this question useful or should I just delete it?

    – bartavelle
    Dec 19 '18 at 15:19








2




2





Heads-up: you have UB here through the pointer cast, but also from the conflicting definitions of _BAR which violate ODR. I think you're knee-deep in a solution which won't pan out.

– Quentin
Dec 19 '18 at 14:05





Heads-up: you have UB here through the pointer cast, but also from the conflicting definitions of _BAR which violate ODR. I think you're knee-deep in a solution which won't pan out.

– Quentin
Dec 19 '18 at 14:05




1




1





each implementation defines its own struct? _BAR has to be always the same type, and you cannot define the same type more than once

– user463035818
Dec 19 '18 at 14:05





each implementation defines its own struct? _BAR has to be always the same type, and you cannot define the same type more than once

– user463035818
Dec 19 '18 at 14:05













Alright, I went with a distinct solution. Is this question useful or should I just delete it?

– bartavelle
Dec 19 '18 at 15:19





Alright, I went with a distinct solution. Is this question useful or should I just delete it?

– bartavelle
Dec 19 '18 at 15:19












2 Answers
2






active

oldest

votes


















1














As mentioned in the comments by @Quentin, this is a violation of ODR (one definition rule). I am not sure what your purpose is, but you can consider 2 things to meet your need. One is using a template. The other one is to use void *. but I am just guessing and hope that helps.






share|improve this answer
























  • Ah I see. I can perhaps use templates, although I am not sure how to solve this. I could make the structures public, and have them have a single void * member to ensure "type safetyness" on the caller side.

    – bartavelle
    Dec 19 '18 at 14:46



















1














I would make an empty structure as a base:



// For base interface
struct _BAR {};

// For an implementation
struct Baz : BAR { ... };


In this case static_cast would do, and it will be legal C++






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53852667%2fusing-single-member-structs-as-the-member-itself%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    As mentioned in the comments by @Quentin, this is a violation of ODR (one definition rule). I am not sure what your purpose is, but you can consider 2 things to meet your need. One is using a template. The other one is to use void *. but I am just guessing and hope that helps.






    share|improve this answer
























    • Ah I see. I can perhaps use templates, although I am not sure how to solve this. I could make the structures public, and have them have a single void * member to ensure "type safetyness" on the caller side.

      – bartavelle
      Dec 19 '18 at 14:46
















    1














    As mentioned in the comments by @Quentin, this is a violation of ODR (one definition rule). I am not sure what your purpose is, but you can consider 2 things to meet your need. One is using a template. The other one is to use void *. but I am just guessing and hope that helps.






    share|improve this answer
























    • Ah I see. I can perhaps use templates, although I am not sure how to solve this. I could make the structures public, and have them have a single void * member to ensure "type safetyness" on the caller side.

      – bartavelle
      Dec 19 '18 at 14:46














    1












    1








    1







    As mentioned in the comments by @Quentin, this is a violation of ODR (one definition rule). I am not sure what your purpose is, but you can consider 2 things to meet your need. One is using a template. The other one is to use void *. but I am just guessing and hope that helps.






    share|improve this answer













    As mentioned in the comments by @Quentin, this is a violation of ODR (one definition rule). I am not sure what your purpose is, but you can consider 2 things to meet your need. One is using a template. The other one is to use void *. but I am just guessing and hope that helps.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Dec 19 '18 at 14:11









    CS PeiCS Pei

    8,4182040




    8,4182040













    • Ah I see. I can perhaps use templates, although I am not sure how to solve this. I could make the structures public, and have them have a single void * member to ensure "type safetyness" on the caller side.

      – bartavelle
      Dec 19 '18 at 14:46



















    • Ah I see. I can perhaps use templates, although I am not sure how to solve this. I could make the structures public, and have them have a single void * member to ensure "type safetyness" on the caller side.

      – bartavelle
      Dec 19 '18 at 14:46

















    Ah I see. I can perhaps use templates, although I am not sure how to solve this. I could make the structures public, and have them have a single void * member to ensure "type safetyness" on the caller side.

    – bartavelle
    Dec 19 '18 at 14:46





    Ah I see. I can perhaps use templates, although I am not sure how to solve this. I could make the structures public, and have them have a single void * member to ensure "type safetyness" on the caller side.

    – bartavelle
    Dec 19 '18 at 14:46













    1














    I would make an empty structure as a base:



    // For base interface
    struct _BAR {};

    // For an implementation
    struct Baz : BAR { ... };


    In this case static_cast would do, and it will be legal C++






    share|improve this answer




























      1














      I would make an empty structure as a base:



      // For base interface
      struct _BAR {};

      // For an implementation
      struct Baz : BAR { ... };


      In this case static_cast would do, and it will be legal C++






      share|improve this answer


























        1












        1








        1







        I would make an empty structure as a base:



        // For base interface
        struct _BAR {};

        // For an implementation
        struct Baz : BAR { ... };


        In this case static_cast would do, and it will be legal C++






        share|improve this answer













        I would make an empty structure as a base:



        // For base interface
        struct _BAR {};

        // For an implementation
        struct Baz : BAR { ... };


        In this case static_cast would do, and it will be legal C++







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 1 at 8:55









        Alexander GutenevAlexander Gutenev

        1,072918




        1,072918






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53852667%2fusing-single-member-structs-as-the-member-itself%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith