Why doesn't “auto ch = unsigned char{'p'}” compile under C++ 17?What is an unsigned char?Why does C++...

Can we use the stored gravitational potential energy of a building to produce power?

Are there any modern advantages of a fire piston?

Am I a Rude Number?

In Linux what happens if 1000 files in a directory are moved to another location while another 300 files were added to the source directory?

Avoiding morning and evening handshakes

Does Windows 10's telemetry include sending *.doc files if Word crashed?

Can a person refuse a presidential pardon?

Magento 2 : Call Helper Without Using __construct in Own Module

Can a hotel cancel a confirmed reservation?

What is this metal M-shaped device for?

Caruana vs Carlsen game 10 (WCC) why not 18...Nxb6?

Injecting creativity into a cookbook

Parsing a string of key-value pairs as a dictionary

How to tag distinct options/entities without giving any an implicit priority or suggested order?

Is there some relative to Dutch word "kijken" in German?

Indirectly access environment variable

Do authors have to be politically correct in article-writing?

What is the lore-based reason that the Spectator has the Create Food and Water trait, instead of simply not requiring food and water?

Explain the objections to these measures against human trafficking

Difference between `vector<int> v;` and `vector<int> v = vector<int>();`

Citing paywalled articles accessed via illegal web sharing

Cryptic with missing capitals

Why Normality assumption in linear regression

Why would space fleets be aligned?



Why doesn't “auto ch = unsigned char{'p'}” compile under C++ 17?


What is an unsigned char?Why does C++ compilation take so long?Why do we need virtual functions in C++?Why is this program erroneously rejected by three C++ compilers?Why should C++ programmers minimize use of 'new'?Why is reading lines from stdin much slower in C++ than Python?g++ gives error : invalid initialization of reference of type ‘char&’ from expression of type ‘unsigned char’basic_string of unsigned char Value TypeError when attempting to static_cast<> const char[] to unsigned char *How do I cast a const char* to a const unsigned char*













6















I'm puzzled. Isn't const auto ch = unsigned char{'p'}; a perfectly valid initialization expression? Fails to be compiled by all three major compilers with almost identical error messages:




error: expected '(' for function-style cast or type construction




Swapping curly braces for ('p') changes nothing.
It does, however, compile without the signed or unsigned keyword.



Online demo.










share|improve this question























  • @FrançoisAndrieux: so does const auto ch = static_cast<unsigned char>('p'), but that's conversion, not initialization.

    – Violet Giraffe
    1 hour ago








  • 1





    using T = unsigned char; const auto ch = T{'p'}; seems to work.

    – François Andrieux
    1 hour ago











  • @FrançoisAndrieux: Hm, do you think the compiler simply fails to parse unsigned char as a single type name in this context?

    – Violet Giraffe
    1 hour ago
















6















I'm puzzled. Isn't const auto ch = unsigned char{'p'}; a perfectly valid initialization expression? Fails to be compiled by all three major compilers with almost identical error messages:




error: expected '(' for function-style cast or type construction




Swapping curly braces for ('p') changes nothing.
It does, however, compile without the signed or unsigned keyword.



Online demo.










share|improve this question























  • @FrançoisAndrieux: so does const auto ch = static_cast<unsigned char>('p'), but that's conversion, not initialization.

    – Violet Giraffe
    1 hour ago








  • 1





    using T = unsigned char; const auto ch = T{'p'}; seems to work.

    – François Andrieux
    1 hour ago











  • @FrançoisAndrieux: Hm, do you think the compiler simply fails to parse unsigned char as a single type name in this context?

    – Violet Giraffe
    1 hour ago














6












6








6








I'm puzzled. Isn't const auto ch = unsigned char{'p'}; a perfectly valid initialization expression? Fails to be compiled by all three major compilers with almost identical error messages:




error: expected '(' for function-style cast or type construction




Swapping curly braces for ('p') changes nothing.
It does, however, compile without the signed or unsigned keyword.



Online demo.










share|improve this question














I'm puzzled. Isn't const auto ch = unsigned char{'p'}; a perfectly valid initialization expression? Fails to be compiled by all three major compilers with almost identical error messages:




error: expected '(' for function-style cast or type construction




Swapping curly braces for ('p') changes nothing.
It does, however, compile without the signed or unsigned keyword.



Online demo.







c++ c++14 c++17






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 1 hour ago









Violet GiraffeViolet Giraffe

14.6k28135247




14.6k28135247













  • @FrançoisAndrieux: so does const auto ch = static_cast<unsigned char>('p'), but that's conversion, not initialization.

    – Violet Giraffe
    1 hour ago








  • 1





    using T = unsigned char; const auto ch = T{'p'}; seems to work.

    – François Andrieux
    1 hour ago











  • @FrançoisAndrieux: Hm, do you think the compiler simply fails to parse unsigned char as a single type name in this context?

    – Violet Giraffe
    1 hour ago



















  • @FrançoisAndrieux: so does const auto ch = static_cast<unsigned char>('p'), but that's conversion, not initialization.

    – Violet Giraffe
    1 hour ago








  • 1





    using T = unsigned char; const auto ch = T{'p'}; seems to work.

    – François Andrieux
    1 hour ago











  • @FrançoisAndrieux: Hm, do you think the compiler simply fails to parse unsigned char as a single type name in this context?

    – Violet Giraffe
    1 hour ago

















@FrançoisAndrieux: so does const auto ch = static_cast<unsigned char>('p'), but that's conversion, not initialization.

– Violet Giraffe
1 hour ago







@FrançoisAndrieux: so does const auto ch = static_cast<unsigned char>('p'), but that's conversion, not initialization.

– Violet Giraffe
1 hour ago






1




1





using T = unsigned char; const auto ch = T{'p'}; seems to work.

– François Andrieux
1 hour ago





using T = unsigned char; const auto ch = T{'p'}; seems to work.

– François Andrieux
1 hour ago













@FrançoisAndrieux: Hm, do you think the compiler simply fails to parse unsigned char as a single type name in this context?

– Violet Giraffe
1 hour ago





@FrançoisAndrieux: Hm, do you think the compiler simply fails to parse unsigned char as a single type name in this context?

– Violet Giraffe
1 hour ago












1 Answer
1






active

oldest

votes


















12














Because only single-word type name could be used for this kind of explicit type conversion.




A single-word type name followed by a braced-init-list is a prvalue of the specified type designating a temporary (until C++17) whose result object is (since C++17) direct-list-initialized with the specified braced-init-list.




unsigned char is not a single-word type name, while char is. And this is true for functional cast expression too, that's why ('p') doesn't work either.



As the workaround, you can



using uc = unsigned char;
const auto ch = uc{'p'};


Or



// change it to c-style cast expression
const auto ch = (unsigned char) 'p';





share|improve this answer





















  • 2





    Do you happen to know the reason for this limitation? Like, if multi-word type names were allowed here, what other things would become broken?

    – Violet Giraffe
    58 mins ago











  • @VioletGiraffe To be honest I don't know; I only know that the standard says so.

    – songyuanyao
    56 mins ago











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%2f54947170%2fwhy-doesnt-auto-ch-unsigned-charp-compile-under-c-17%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









12














Because only single-word type name could be used for this kind of explicit type conversion.




A single-word type name followed by a braced-init-list is a prvalue of the specified type designating a temporary (until C++17) whose result object is (since C++17) direct-list-initialized with the specified braced-init-list.




unsigned char is not a single-word type name, while char is. And this is true for functional cast expression too, that's why ('p') doesn't work either.



As the workaround, you can



using uc = unsigned char;
const auto ch = uc{'p'};


Or



// change it to c-style cast expression
const auto ch = (unsigned char) 'p';





share|improve this answer





















  • 2





    Do you happen to know the reason for this limitation? Like, if multi-word type names were allowed here, what other things would become broken?

    – Violet Giraffe
    58 mins ago











  • @VioletGiraffe To be honest I don't know; I only know that the standard says so.

    – songyuanyao
    56 mins ago
















12














Because only single-word type name could be used for this kind of explicit type conversion.




A single-word type name followed by a braced-init-list is a prvalue of the specified type designating a temporary (until C++17) whose result object is (since C++17) direct-list-initialized with the specified braced-init-list.




unsigned char is not a single-word type name, while char is. And this is true for functional cast expression too, that's why ('p') doesn't work either.



As the workaround, you can



using uc = unsigned char;
const auto ch = uc{'p'};


Or



// change it to c-style cast expression
const auto ch = (unsigned char) 'p';





share|improve this answer





















  • 2





    Do you happen to know the reason for this limitation? Like, if multi-word type names were allowed here, what other things would become broken?

    – Violet Giraffe
    58 mins ago











  • @VioletGiraffe To be honest I don't know; I only know that the standard says so.

    – songyuanyao
    56 mins ago














12












12








12







Because only single-word type name could be used for this kind of explicit type conversion.




A single-word type name followed by a braced-init-list is a prvalue of the specified type designating a temporary (until C++17) whose result object is (since C++17) direct-list-initialized with the specified braced-init-list.




unsigned char is not a single-word type name, while char is. And this is true for functional cast expression too, that's why ('p') doesn't work either.



As the workaround, you can



using uc = unsigned char;
const auto ch = uc{'p'};


Or



// change it to c-style cast expression
const auto ch = (unsigned char) 'p';





share|improve this answer















Because only single-word type name could be used for this kind of explicit type conversion.




A single-word type name followed by a braced-init-list is a prvalue of the specified type designating a temporary (until C++17) whose result object is (since C++17) direct-list-initialized with the specified braced-init-list.




unsigned char is not a single-word type name, while char is. And this is true for functional cast expression too, that's why ('p') doesn't work either.



As the workaround, you can



using uc = unsigned char;
const auto ch = uc{'p'};


Or



// change it to c-style cast expression
const auto ch = (unsigned char) 'p';






share|improve this answer














share|improve this answer



share|improve this answer








edited 35 mins ago

























answered 1 hour ago









songyuanyaosongyuanyao

92.2k11175242




92.2k11175242








  • 2





    Do you happen to know the reason for this limitation? Like, if multi-word type names were allowed here, what other things would become broken?

    – Violet Giraffe
    58 mins ago











  • @VioletGiraffe To be honest I don't know; I only know that the standard says so.

    – songyuanyao
    56 mins ago














  • 2





    Do you happen to know the reason for this limitation? Like, if multi-word type names were allowed here, what other things would become broken?

    – Violet Giraffe
    58 mins ago











  • @VioletGiraffe To be honest I don't know; I only know that the standard says so.

    – songyuanyao
    56 mins ago








2




2





Do you happen to know the reason for this limitation? Like, if multi-word type names were allowed here, what other things would become broken?

– Violet Giraffe
58 mins ago





Do you happen to know the reason for this limitation? Like, if multi-word type names were allowed here, what other things would become broken?

– Violet Giraffe
58 mins ago













@VioletGiraffe To be honest I don't know; I only know that the standard says so.

– songyuanyao
56 mins ago





@VioletGiraffe To be honest I don't know; I only know that the standard says so.

– songyuanyao
56 mins ago




















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%2f54947170%2fwhy-doesnt-auto-ch-unsigned-charp-compile-under-c-17%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

Benedict Cumberbatch Contingut Inicis Debut professional Premis Filmografia bàsica Premis i...

Monticle de plataforma Contingut Est de Nord Amèrica Interpretacions Altres cultures Vegeu...

Escacs Janus Enllaços externs Menú de navegacióEscacs JanusJanusschachBrainKing.comChessV