Using single-member structs as the member itself
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++
add a comment |
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++
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
add a comment |
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++
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++
c++ visual-c++
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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.
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 singlevoid *
member to ensure "type safetyness" on the caller side.
– bartavelle
Dec 19 '18 at 14:46
add a comment |
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++
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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 singlevoid *
member to ensure "type safetyness" on the caller side.
– bartavelle
Dec 19 '18 at 14:46
add a comment |
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.
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 singlevoid *
member to ensure "type safetyness" on the caller side.
– bartavelle
Dec 19 '18 at 14:46
add a comment |
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.
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.
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 singlevoid *
member to ensure "type safetyness" on the caller side.
– bartavelle
Dec 19 '18 at 14:46
add a comment |
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 singlevoid *
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
add a comment |
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++
add a comment |
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++
add a comment |
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++
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++
answered Jan 1 at 8:55


Alexander GutenevAlexander Gutenev
1,072918
1,072918
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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