473,729 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why can I test for is_volatile<T> but I can't test for is_unsigned<T>?

Why do I get a syntax error below?

I don't see why volatile works but unsigned does not work.

I'm not looking for an answer of the form, "Because the Standard says
so", or "Because the C++ grammer says so"; I'm looking for an
explanation of *why* the Standard and/or the grammar say so.

Thanks,

Robert Schwartz


#include <iostream>

template <typename T>
class is_volatile/*<T>*/
{ public: static const bool value = false; };

template <typename T>
class is_volatile<vol atile T>
{ public: static const bool value = true; };

template <typename T>
void test1(void)
{
std::cout << is_volatile<T>: :value << std::endl;
}

template <typename T>
class is_unsigned/*<T>*/
{ public: static const bool value = false; };

template <typename T>
class is_unsigned<uns igned T> // "syntax error before '>' token" on
this line.
{ public: static const bool value = true; };

template <typename T>
void test2(void)
{
std::cout << is_unsigned<T>: :value << std::endl;
}

int main(void)
{
test1<volatile int>();

test2<unsigned int>();

return 0;
}
Jul 22 '05 #1
7 1537
Robert Allan Schwartz wrote:
Why do I get a syntax error below?

I don't see why volatile works but unsigned does not work.

I'm not looking for an answer of the form, "Because the Standard says
so", or "Because the C++ grammer says so"; I'm looking for an
explanation of *why* the Standard and/or the grammar say so.
IANALG but I think that unsigned is part of the type itself rather than
a qualifier.
Since there are only three (maybe four) unsigned types, why not do the
below:

#include <iostream>

template <typename T>
class is_volatile/*<T>*/
{ public: static const bool value = false; };

template <typename T>
class is_volatile<vol atile T>
{ public: static const bool value = true; };

template <typename T>
void test1(void)
{
std::cout << is_volatile<T>: :value << std::endl;
}

template <typename T>
class is_unsigned/*<T>*/
{ public: static const bool value = false; };

template <typename T>
class is_unsigned<uns igned T> // "syntax error before '>' token" on
this line.
{ public: static const bool value = true; };

template <>
class is_unsigned<uns igned char>
{ public: static const bool value = true; };

#ifdef UNSIGNED_INT_IS _A_DISTINCT_TYP E_
template <>
class is_unsigned<uns igned int> // not sure if this is needed
this line.
{ public: static const bool value = true; };
#endif

template <>
class is_unsigned<uns igned short>
this line.
{ public: static const bool value = true; };

template <>
class is_unsigned<uns igned long>
this line.
{ public: static const bool value = true; };

#ifdef UNSIGNED_LONG_L ONG_IS_DEFINED_ BY_THE_COMPILER _
template <>
class is_unsigned<uns igned long long>
this line.
{ public: static const bool value = true; };
#endif
template <typename T>
void test2(void)
{
std::cout << is_unsigned<T>: :value << std::endl;
}

int main(void)
{
test1<volatile int>();

test2<unsigned int>();

return 0;
}

Jul 22 '05 #2
On Wed, 11 Aug 2004 11:12:56 -0700, Robert Allan Schwartz wrote:
I don't see why volatile works but unsigned does not work.
As noted by other(s), volatile (like const) is a type
qualifier. 'unsigned' is a part of the type.
template <typename T>
class is_unsigned/*<T>*/
{ public: static const bool value = false; };


You can implement is_unsigned in terms of std::numeric_li mits:

#include <iostream>
#include <limits>

int main(void)
{
std::cout << std::numeric_li mits<int>::is_s igned << '\n';
}

Ali
Jul 22 '05 #3
I understand that there is another way to implement is_unsigned<T>.
I understand that 'volatile' is a qualifier and 'unsigned' is part of
the type.
What I want to know is, why can I ask type questions of the form
'is_[qualifier]<T>', but I cannot ask type questions of the form
'is_[part_of_type]<T>'?
Did the Standards Committee decide to allow the former and disallow
the latter?
Is this simply an artifact of the grammar?
I'm looking for a deeper explanation.
Anyone got one?

Thanks,

Robert

red floyd <no*****@here.d ude> wrote in message news:<8u******* **********@news svr21.news.prod igy.com>...
Robert Allan Schwartz wrote:
Why do I get a syntax error below?

I don't see why volatile works but unsigned does not work.

I'm not looking for an answer of the form, "Because the Standard says
so", or "Because the C++ grammer says so"; I'm looking for an
explanation of *why* the Standard and/or the grammar say so.

IANALG but I think that unsigned is part of the type itself rather than
a qualifier.
Since there are only three (maybe four) unsigned types, why not do the
below:

#include <iostream>

template <typename T>
class is_volatile/*<T>*/
{ public: static const bool value = false; };

template <typename T>
class is_volatile<vol atile T>
{ public: static const bool value = true; };

template <typename T>
void test1(void)
{
std::cout << is_volatile<T>: :value << std::endl;
}

template <typename T>
class is_unsigned/*<T>*/
{ public: static const bool value = false; };

template <typename T>
class is_unsigned<uns igned T> // "syntax error before '>' token" on
this line.
{ public: static const bool value = true; };

template <>
class is_unsigned<uns igned char>
{ public: static const bool value = true; };

#ifdef UNSIGNED_INT_IS _A_DISTINCT_TYP E_
template <>
class is_unsigned<uns igned int> // not sure if this is needed
this line.
{ public: static const bool value = true; };
#endif

template <>
class is_unsigned<uns igned short>
this line.
{ public: static const bool value = true; };

template <>
class is_unsigned<uns igned long>
this line.
{ public: static const bool value = true; };

#ifdef UNSIGNED_LONG_L ONG_IS_DEFINED_ BY_THE_COMPILER _
template <>
class is_unsigned<uns igned long long>
this line.
{ public: static const bool value = true; };
#endif
>
template <typename T>
void test2(void)
{
std::cout << is_unsigned<T>: :value << std::endl;
}

int main(void)
{
test1<volatile int>();

test2<unsigned int>();

return 0;
}

Jul 22 '05 #4

"Robert Allan Schwartz" <no****@tessell ation.com> wrote in message
news:11******** *************** ***@posting.goo gle.com...
I understand that there is another way to implement is_unsigned<T>.
I understand that 'volatile' is a qualifier and 'unsigned' is part of the type.
What I want to know is, why can I ask type questions of the form
'is_[qualifier]<T>', but I cannot ask type questions of the form
'is_[part_of_type]<T>'?
You can ask both types of questions, as the responses to your post
have shown.
Did the Standards Committee decide to allow the former and disallow
the latter?
Is this simply an artifact of the grammar?
I'm looking for a deeper explanation.


What we need is a deeper explanation of you question.

Jonathan
Jul 22 '05 #5
Robert Allan Schwartz wrote:
I understand that there is another way to implement is_unsigned<T>.
I understand that 'volatile' is a qualifier and 'unsigned' is part of
the type.
What I want to know is, why can I ask type questions of the form
'is_[qualifier]<T>', but I cannot ask type questions of the form
'is_[part_of_type]<T>'?
Did the Standards Committee decide to allow the former and disallow
the latter?
Is this simply an artifact of the grammar?
I'm looking for a deeper explanation.
Anyone got one?


It is the grammar. You should consider it an accident that the typename
"unisgned int" consists of two words. After lexical analysis in the
compiler, very likely, this name becomes just one token. This is, when
the compiler deals with all the template magic, the information you want
is lost already.

What you are trying to do is like writing a template that tests whether a
typename starts with the letter "i".

However, since "unsigned" is not a qualifier, there are no user-defined
types that are unsigned or signed. Thus your template is meaningfull only
for a small number of basic types. For these you can provide specializations
and that way build is_unsigned<T> so that it checks true for the unsigned
basic types. But then, again, this has been done already in numeric_limits.
Best

Kai-Uwe
Jul 22 '05 #6
Let me try to refine my question.

In many cases, I can ask if a type is composed of two parts: a token
(e.g. * or &) or a keyword (e.g. const or volatile), followed by
another type.

I can ask if a type is "pointer to T".
I can ask if a type is "reference to T".
I can ask if a type is "const T".
I can ask if a type is "volatile T".
I cannot ask if a type is "unsigned T".
^^^

This appears to be an asymmetry.

Why is there an asymmetry?

I understand that there is another way to ask is_unsigned<T>. That
does not answer my question.
I understand that 'volatile' is a qualifier and 'unsigned' is not.
That does not answer my question.

Thanks,

Robert

"Jonathan Turkanis" <te******@kanga roologic.com> wrote in message news:<2o******* *****@uni-berlin.de>...
"Robert Allan Schwartz" <no****@tessell ation.com> wrote in message
news:11******** *************** ***@posting.goo gle.com...
I understand that there is another way to implement is_unsigned<T>.
I understand that 'volatile' is a qualifier and 'unsigned' is part

of
the type.
What I want to know is, why can I ask type questions of the form
'is_[qualifier]<T>', but I cannot ask type questions of the form
'is_[part_of_type]<T>'?


You can ask both types of questions, as the responses to your post
have shown.
Did the Standards Committee decide to allow the former and disallow
the latter?
Is this simply an artifact of the grammar?
I'm looking for a deeper explanation.


What we need is a deeper explanation of you question.

Jonathan

Jul 22 '05 #7
Robert Allan Schwartz wrote:
Let me try to refine my question.

In many cases, I can ask if a type is composed of two parts: a token
(e.g. * or &) or a keyword (e.g. const or volatile), followed by
another type.

I can ask if a type is "pointer to T".
I can ask if a type is "reference to T".
I can ask if a type is "const T".
I can ask if a type is "volatile T".
I cannot ask if a type is "unsigned T".
^^^

This appears to be an asymmetry.


You can view this as a crack in the *lexical* structure of C++.
"unsigned int" should rather be "unsigned_i nt" but for compatibility
reasons to C they kept it the other way. Now, since the thing is only
an entity of the lexical structure, you must understand, that for the
compiler "unsigned int" actually reads as "unsigned_i nt", the parser
might get an appropriate token from the lexical scanner..."unsi gned T"
can't be understood, because the comprehension would need to happen at
a semantic level (where T is identified as a type) -- on a lexical
level it doesn't make sense and will never make it into the parser.

Okay, the ones in charge could have promoted "unsigned" out of the
lowland of lexical scanning to the semantic pastures (just to correct
the unpleasant visual artifact) but that would have made everything
much more complicated: every part that deals with cv-qualifiers would
be forced to care for the completely uninteresting, dull and usually
(except for a couple of primitive cases) inapplicable case of
"unsigned". An extra qualifier would burden the user as well. For
example, if you want to do something like "remove_qualifi er" you'll
have to check 4 different combinations of qualifiers, adding unsigned
would make you have 8 cases to check!! Qualifiers multiply the number
of possible qualifiers Types, so it's better to keep only the useful
ones. Read this as: We really don't need "unsigned T", thus we don't
commit anybody to parse it.

Marco

Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3224
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are writing one object in C# which will take the entire table tag contents and renders. Ie., we need to pass "<table>………… <thead>……</thead>. <tr>.<td> <td>..<tr>.<td> <td> </table>" content to
8
6886
by: Michael | last post by:
This is a two-part question to which I haven't been able to find an answer anywhere else. 1. Is it possible to format the bullet/number character of the <li>? In my styles sheet, I have the <li> tag formatted, for example, bold. However, when it's applied, the number of the <li> is not bold, but the text is. Do I have to apply the bold to the <ol> instead? 2. When I use <li>example text</li>, and when I insert a <br> after the </li>,...
2
11230
by: js | last post by:
I have a table rendered with XSLT. The first column has a radio button controls for user to make a selection for a particular row. All the values in the remaining columns are all concated with a &#xA0; (blank). I need to retieve the text in each cell of that row if the radio button on that row is clicked. Each <TD> has a numeric ID so that I can use it with document.getElementById().innerText method. I use Javascript to assign some...
9
6840
by: Wang, Jay | last post by:
Hello, all, I would like to enable some text between <SPAN url="http://www.testserver.com/">WORD TO BE DRAGGED </SPAN>. I put some javascript and it will extract http://www.testserver.com/ from the the span element when I select the whole text in the SPAN and drag it. However, I want to drag it without have to select the words between the span element. The default mouse action will only select the words when i move the mouse. Can...
2
15949
by: ESPNSTI | last post by:
Hi, I'm trying to use a generics dictionary with a key class that implements and needs IComparable<>. However when I attempt to use the dictionary, it doesn't appear to use the IComparable<> to find the key. In the example below, accessing the dictionary by using the exact key object that was used to add to the dictionary works. (see code comment 1). However, if I attempt to access the dictionary by using a key object that
11
2235
by: Holger | last post by:
Hi I have not been able to figure out how to do compound statement from C - "<test>?<true-val>:<false-val>" But something similar must exist...?! I would like to do the equivalent if python of the C line: printf("I saw %d car%s\n", n, n != 1 ? "s" : "")
7
2791
by: Xiaoyan | last post by:
Hi,everyone: I have a problem now. I can't get the information between the <tr><td> and </td></tr>. for example: I use this regular expression can't get it, I don't know why. $test=~/<tr><td>(.*)<\/td><\/td>(.*)<\/td><\/tr>/ms; <tr><td>station</td> <td>station number/identification, see chart above: <br> B = GoMoos buoy B location<br> S = New Scantum at the southern edge Jefferys Ledge</td></tr>.
36
5103
by: Roedy Green | last post by:
The only browser I have encountered that supports <colgroup><col class="behold"></colgroup> to apply a CSS style to a whole column, is Microsoft Internet Explorer. I have been told it SHOULD NOT do so, since this is not part of the specification. How then to you apply styles to entire columns? Surely you don't have to write <td class="behold"on every row item.
2
2268
dlite922
by: dlite922 | last post by:
Hello Hello! I'm trying to connect to a host that accepts UDP on a port I setup and tested from a command line too called udp_write. However a PHP fsocketopen() call doesn't work nor does it give an error. stripped down code + fake data: $fp = fsockopen("udp://999.999.999.999",9999,$errNo,$errMsg) or die("$errNo : $errMsg"); $test = fwrite($fp, "Logging Test");
0
8921
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8763
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9202
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9148
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8151
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4796
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2683
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2165
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.