Initialise a reference member to C array don't compile on visual studio 2015
When I try to compile the code below on g++ it's work but on vs2015 it's fail with message :
error C2440: 'initializing': cannot convert from 'const bool *' to 'const bool (&)[3]'
#include <iostream>
enum class Direction
{
RIGTH,
LEFT
};
struct Buffer
{
int catRigth = 4;
int catLeft = 8;
bool dogRigth[3] = {true, false, true};
bool dogLeft[3] = {false, true, false};
};
struct Bind
{
const int &cat;
const bool (&dog)[3];
Bind(const Buffer &buf, Direction direction) :
cat(direction == Direction::RIGTH ? buf.catRigth : buf.catLeft),
dog(direction == Direction::RIGTH ? buf.dogRigth : buf.dogLeft)
{
}
};
int main(int argc, char* argv)
{
const Buffer buff;
Bind bindRigth(buff, Direction::RIGTH);
Bind bindLeft(buff, Direction::LEFT);
int catRigth = bindRigth.cat;
int catLeft = bindLeft.cat;
std::cout << catRigth << " " << catLeft;
}
Is it a standard C++ code or it's gcc specific comportment?
c++ visual-studio-2015
|
show 4 more comments
When I try to compile the code below on g++ it's work but on vs2015 it's fail with message :
error C2440: 'initializing': cannot convert from 'const bool *' to 'const bool (&)[3]'
#include <iostream>
enum class Direction
{
RIGTH,
LEFT
};
struct Buffer
{
int catRigth = 4;
int catLeft = 8;
bool dogRigth[3] = {true, false, true};
bool dogLeft[3] = {false, true, false};
};
struct Bind
{
const int &cat;
const bool (&dog)[3];
Bind(const Buffer &buf, Direction direction) :
cat(direction == Direction::RIGTH ? buf.catRigth : buf.catLeft),
dog(direction == Direction::RIGTH ? buf.dogRigth : buf.dogLeft)
{
}
};
int main(int argc, char* argv)
{
const Buffer buff;
Bind bindRigth(buff, Direction::RIGTH);
Bind bindLeft(buff, Direction::LEFT);
int catRigth = bindRigth.cat;
int catLeft = bindLeft.cat;
std::cout << catRigth << " " << catLeft;
}
Is it a standard C++ code or it's gcc specific comportment?
c++ visual-studio-2015
Or in other words: does the conditional operator trigger array-to-pointer decay?
– Quentin
Nov 23 '18 at 10:38
Seems like only MSVC has this behavior. clang, gcc and icc work as "expected"
– Matthieu Brucher
Nov 23 '18 at 10:39
Initialize it like this: _dog { direction == Direction::RIGTH ? buf.dogRigth : buf.dogLeft)} {}
– snake_style
Nov 23 '18 at 11:03
Tried your code out on cpp.sh, it complained about obj not being initialized. Did that, worked then. Maybe doing that makes it work on vs2015 too? (Altered code: cpp.sh/3wqyg ) In any case ugly code, why don't you simply use a pointer?
– Aziuth
Nov 23 '18 at 11:39
Sorry I accepted the edit by Felix when I probably don't do, I'm new here. I tested my code on real vs and it don't compiled.
– Gurdil
Nov 23 '18 at 11:57
|
show 4 more comments
When I try to compile the code below on g++ it's work but on vs2015 it's fail with message :
error C2440: 'initializing': cannot convert from 'const bool *' to 'const bool (&)[3]'
#include <iostream>
enum class Direction
{
RIGTH,
LEFT
};
struct Buffer
{
int catRigth = 4;
int catLeft = 8;
bool dogRigth[3] = {true, false, true};
bool dogLeft[3] = {false, true, false};
};
struct Bind
{
const int &cat;
const bool (&dog)[3];
Bind(const Buffer &buf, Direction direction) :
cat(direction == Direction::RIGTH ? buf.catRigth : buf.catLeft),
dog(direction == Direction::RIGTH ? buf.dogRigth : buf.dogLeft)
{
}
};
int main(int argc, char* argv)
{
const Buffer buff;
Bind bindRigth(buff, Direction::RIGTH);
Bind bindLeft(buff, Direction::LEFT);
int catRigth = bindRigth.cat;
int catLeft = bindLeft.cat;
std::cout << catRigth << " " << catLeft;
}
Is it a standard C++ code or it's gcc specific comportment?
c++ visual-studio-2015
When I try to compile the code below on g++ it's work but on vs2015 it's fail with message :
error C2440: 'initializing': cannot convert from 'const bool *' to 'const bool (&)[3]'
#include <iostream>
enum class Direction
{
RIGTH,
LEFT
};
struct Buffer
{
int catRigth = 4;
int catLeft = 8;
bool dogRigth[3] = {true, false, true};
bool dogLeft[3] = {false, true, false};
};
struct Bind
{
const int &cat;
const bool (&dog)[3];
Bind(const Buffer &buf, Direction direction) :
cat(direction == Direction::RIGTH ? buf.catRigth : buf.catLeft),
dog(direction == Direction::RIGTH ? buf.dogRigth : buf.dogLeft)
{
}
};
int main(int argc, char* argv)
{
const Buffer buff;
Bind bindRigth(buff, Direction::RIGTH);
Bind bindLeft(buff, Direction::LEFT);
int catRigth = bindRigth.cat;
int catLeft = bindLeft.cat;
std::cout << catRigth << " " << catLeft;
}
Is it a standard C++ code or it's gcc specific comportment?
c++ visual-studio-2015
c++ visual-studio-2015
edited Nov 23 '18 at 12:23
Gurdil
asked Nov 23 '18 at 10:33
GurdilGurdil
186
186
Or in other words: does the conditional operator trigger array-to-pointer decay?
– Quentin
Nov 23 '18 at 10:38
Seems like only MSVC has this behavior. clang, gcc and icc work as "expected"
– Matthieu Brucher
Nov 23 '18 at 10:39
Initialize it like this: _dog { direction == Direction::RIGTH ? buf.dogRigth : buf.dogLeft)} {}
– snake_style
Nov 23 '18 at 11:03
Tried your code out on cpp.sh, it complained about obj not being initialized. Did that, worked then. Maybe doing that makes it work on vs2015 too? (Altered code: cpp.sh/3wqyg ) In any case ugly code, why don't you simply use a pointer?
– Aziuth
Nov 23 '18 at 11:39
Sorry I accepted the edit by Felix when I probably don't do, I'm new here. I tested my code on real vs and it don't compiled.
– Gurdil
Nov 23 '18 at 11:57
|
show 4 more comments
Or in other words: does the conditional operator trigger array-to-pointer decay?
– Quentin
Nov 23 '18 at 10:38
Seems like only MSVC has this behavior. clang, gcc and icc work as "expected"
– Matthieu Brucher
Nov 23 '18 at 10:39
Initialize it like this: _dog { direction == Direction::RIGTH ? buf.dogRigth : buf.dogLeft)} {}
– snake_style
Nov 23 '18 at 11:03
Tried your code out on cpp.sh, it complained about obj not being initialized. Did that, worked then. Maybe doing that makes it work on vs2015 too? (Altered code: cpp.sh/3wqyg ) In any case ugly code, why don't you simply use a pointer?
– Aziuth
Nov 23 '18 at 11:39
Sorry I accepted the edit by Felix when I probably don't do, I'm new here. I tested my code on real vs and it don't compiled.
– Gurdil
Nov 23 '18 at 11:57
Or in other words: does the conditional operator trigger array-to-pointer decay?
– Quentin
Nov 23 '18 at 10:38
Or in other words: does the conditional operator trigger array-to-pointer decay?
– Quentin
Nov 23 '18 at 10:38
Seems like only MSVC has this behavior. clang, gcc and icc work as "expected"
– Matthieu Brucher
Nov 23 '18 at 10:39
Seems like only MSVC has this behavior. clang, gcc and icc work as "expected"
– Matthieu Brucher
Nov 23 '18 at 10:39
Initialize it like this: _dog { direction == Direction::RIGTH ? buf.dogRigth : buf.dogLeft)} {}
– snake_style
Nov 23 '18 at 11:03
Initialize it like this: _dog { direction == Direction::RIGTH ? buf.dogRigth : buf.dogLeft)} {}
– snake_style
Nov 23 '18 at 11:03
Tried your code out on cpp.sh, it complained about obj not being initialized. Did that, worked then. Maybe doing that makes it work on vs2015 too? (Altered code: cpp.sh/3wqyg ) In any case ugly code, why don't you simply use a pointer?
– Aziuth
Nov 23 '18 at 11:39
Tried your code out on cpp.sh, it complained about obj not being initialized. Did that, worked then. Maybe doing that makes it work on vs2015 too? (Altered code: cpp.sh/3wqyg ) In any case ugly code, why don't you simply use a pointer?
– Aziuth
Nov 23 '18 at 11:39
Sorry I accepted the edit by Felix when I probably don't do, I'm new here. I tested my code on real vs and it don't compiled.
– Gurdil
Nov 23 '18 at 11:57
Sorry I accepted the edit by Felix when I probably don't do, I'm new here. I tested my code on real vs and it don't compiled.
– Gurdil
Nov 23 '18 at 11:57
|
show 4 more comments
1 Answer
1
active
oldest
votes
MSVC shouldn't have decayed its type to const bool *
:
5.16.4 If the second and third operands are glvalues of the same value category and have the same type, the result is of that type and value category and it is a bit-field if the second or the third operand is a bit-field, or if both are bit-fields.
A workaround for MSVC could be:
#include <utility>
const struct A {
bool a[3] = {false};
} obj;
template <class Lhs, class Rhs>
auto &&Conditional(const bool x, Lhs &&lhs, Rhs &&rhs) {
if (x)
return std::forward<Lhs>(lhs);
return std::forward<Rhs>(rhs);
}
int main(int argc, char* argv) {
const bool (&t)[3] = Conditional(true, obj.a, obj.a);
return 0;
}
PS: Conditional isn't a constexpr function.
Or: const bool (&t)[3] = *(true ? &obj.a : &obj.a);
Thanks your solution to reference on ternary and dereference the result do the job for me
– Gurdil
Nov 23 '18 at 13:01
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%2f53445018%2finitialise-a-reference-member-to-c-array-dont-compile-on-visual-studio-2015%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
MSVC shouldn't have decayed its type to const bool *
:
5.16.4 If the second and third operands are glvalues of the same value category and have the same type, the result is of that type and value category and it is a bit-field if the second or the third operand is a bit-field, or if both are bit-fields.
A workaround for MSVC could be:
#include <utility>
const struct A {
bool a[3] = {false};
} obj;
template <class Lhs, class Rhs>
auto &&Conditional(const bool x, Lhs &&lhs, Rhs &&rhs) {
if (x)
return std::forward<Lhs>(lhs);
return std::forward<Rhs>(rhs);
}
int main(int argc, char* argv) {
const bool (&t)[3] = Conditional(true, obj.a, obj.a);
return 0;
}
PS: Conditional isn't a constexpr function.
Or: const bool (&t)[3] = *(true ? &obj.a : &obj.a);
Thanks your solution to reference on ternary and dereference the result do the job for me
– Gurdil
Nov 23 '18 at 13:01
add a comment |
MSVC shouldn't have decayed its type to const bool *
:
5.16.4 If the second and third operands are glvalues of the same value category and have the same type, the result is of that type and value category and it is a bit-field if the second or the third operand is a bit-field, or if both are bit-fields.
A workaround for MSVC could be:
#include <utility>
const struct A {
bool a[3] = {false};
} obj;
template <class Lhs, class Rhs>
auto &&Conditional(const bool x, Lhs &&lhs, Rhs &&rhs) {
if (x)
return std::forward<Lhs>(lhs);
return std::forward<Rhs>(rhs);
}
int main(int argc, char* argv) {
const bool (&t)[3] = Conditional(true, obj.a, obj.a);
return 0;
}
PS: Conditional isn't a constexpr function.
Or: const bool (&t)[3] = *(true ? &obj.a : &obj.a);
Thanks your solution to reference on ternary and dereference the result do the job for me
– Gurdil
Nov 23 '18 at 13:01
add a comment |
MSVC shouldn't have decayed its type to const bool *
:
5.16.4 If the second and third operands are glvalues of the same value category and have the same type, the result is of that type and value category and it is a bit-field if the second or the third operand is a bit-field, or if both are bit-fields.
A workaround for MSVC could be:
#include <utility>
const struct A {
bool a[3] = {false};
} obj;
template <class Lhs, class Rhs>
auto &&Conditional(const bool x, Lhs &&lhs, Rhs &&rhs) {
if (x)
return std::forward<Lhs>(lhs);
return std::forward<Rhs>(rhs);
}
int main(int argc, char* argv) {
const bool (&t)[3] = Conditional(true, obj.a, obj.a);
return 0;
}
PS: Conditional isn't a constexpr function.
Or: const bool (&t)[3] = *(true ? &obj.a : &obj.a);
MSVC shouldn't have decayed its type to const bool *
:
5.16.4 If the second and third operands are glvalues of the same value category and have the same type, the result is of that type and value category and it is a bit-field if the second or the third operand is a bit-field, or if both are bit-fields.
A workaround for MSVC could be:
#include <utility>
const struct A {
bool a[3] = {false};
} obj;
template <class Lhs, class Rhs>
auto &&Conditional(const bool x, Lhs &&lhs, Rhs &&rhs) {
if (x)
return std::forward<Lhs>(lhs);
return std::forward<Rhs>(rhs);
}
int main(int argc, char* argv) {
const bool (&t)[3] = Conditional(true, obj.a, obj.a);
return 0;
}
PS: Conditional isn't a constexpr function.
Or: const bool (&t)[3] = *(true ? &obj.a : &obj.a);
edited Nov 23 '18 at 12:56
answered Nov 23 '18 at 12:38
felixfelix
1,510314
1,510314
Thanks your solution to reference on ternary and dereference the result do the job for me
– Gurdil
Nov 23 '18 at 13:01
add a comment |
Thanks your solution to reference on ternary and dereference the result do the job for me
– Gurdil
Nov 23 '18 at 13:01
Thanks your solution to reference on ternary and dereference the result do the job for me
– Gurdil
Nov 23 '18 at 13:01
Thanks your solution to reference on ternary and dereference the result do the job for me
– Gurdil
Nov 23 '18 at 13:01
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%2f53445018%2finitialise-a-reference-member-to-c-array-dont-compile-on-visual-studio-2015%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
Or in other words: does the conditional operator trigger array-to-pointer decay?
– Quentin
Nov 23 '18 at 10:38
Seems like only MSVC has this behavior. clang, gcc and icc work as "expected"
– Matthieu Brucher
Nov 23 '18 at 10:39
Initialize it like this: _dog { direction == Direction::RIGTH ? buf.dogRigth : buf.dogLeft)} {}
– snake_style
Nov 23 '18 at 11:03
Tried your code out on cpp.sh, it complained about obj not being initialized. Did that, worked then. Maybe doing that makes it work on vs2015 too? (Altered code: cpp.sh/3wqyg ) In any case ugly code, why don't you simply use a pointer?
– Aziuth
Nov 23 '18 at 11:39
Sorry I accepted the edit by Felix when I probably don't do, I'm new here. I tested my code on real vs and it don't compiled.
– Gurdil
Nov 23 '18 at 11:57