473,785 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about "substituti on failure is not an error" concept, SFINAE

The following code can be compiled without error:

typedef char True; // sizeof(True) == 1
typedef struct { char a[2]; } False; // sizeof(False) 1
//...
template <typename TTrue isPtr( T * );
template <typename TFalse isPtr(T);

#define is_ptr( e ) (sizeof(isPtr(e ))==sizeof(True ))

int main() {
int *a;
cout << is_ptr(a) << endl;
}

why the following can compile:
int test();
int main() {
cout << sizeof(test);
}
Nov 29 '07 #1
4 1612
On Nov 29, 2:38 am, Aries Sun <sun.ar...@gmai l.comwrote:
The following code can be compiled without error:

typedef char True; // sizeof(True) == 1
typedef struct { char a[2]; } False; // sizeof(False) 1
//...
template <typename TTrue isPtr( T * );
template <typename TFalse isPtr(T);

#define is_ptr( e ) (sizeof(isPtr(e ))==sizeof(True ))

int main() {
int *a;
cout << is_ptr(a) << endl;

}

why the following can compile:
int test();
int main() {
cout << sizeof(test);

}
I got, because is call sizeof(isPtr(e) ), there is a "(" and ")"
Nov 29 '07 #2
On Wed, 28 Nov 2007 23:38:19 -0800 (PST), Aries Sun wrote:
The following code can be compiled without error:

typedef char True; // sizeof(True) == 1
typedef struct { char a[2]; } False; // sizeof(False) 1
//...
template <typename TTrue isPtr( T * );
template <typename TFalse isPtr(T);

#define is_ptr( e ) (sizeof(isPtr(e ))==sizeof(True ))
Note that normally you would do that like this:

#include <iostream>

struct true_type { enum { result = true }; };
struct false_type { enum { result = false }; };

template<typena me T>
struct is_pointer: public false_type
{
};

template<typena me T>
struct is_pointer<T*>: public true_type
{
};

int main()
{
std::cout << "char* : " << is_pointer<char *>::result << std::endl;
std::cout << "int : " << is_pointer<int> ::result << std::endl;
}

Because true_type and false_type do not have a common anchestor, you
can now also do something like this:

template<typena me T>
void handle_somethin g_do(const T& t, false_type); // for non-pointer cases
template<typena me T>
void handle_somethin g_do(const T& t, true_type); // for pointer cases

void handle_somethin g(const T& t)
{
handle_somethin g_do(t, is_pointer<T>() );
}

Especially doing a sizeof on a function's return value is somewhat
weird in my opinion. Does the function get called or not? If it
does, what does the function do? How should you implement it? Why
should it take room in the executable, when all that is done is
done at compile time?

--
Joel Yliluoma - http://iki.fi/bisqwit/
Nov 29 '07 #3
On Nov 29, 9:15 am, Joel Yliluoma <bisq...@iki.fi wrote:
>
Especially doing a sizeof on a function's return value is somewhat
weird in my opinion. Does the function get called or not?
It doesn't get called - Alexandrescu uses this trick in one of his
books, and it seems fairly common in TMP.
Your way is more elegant though.
Nov 29 '07 #4
On Thu, 29 Nov 2007 04:29:39 -0800 (PST), tragomaskhalos wrote:
On Nov 29, 9:15 am, Joel Yliluoma <bisq...@iki.fi wrote:
>>
Especially doing a sizeof on a function's return value is somewhat
weird in my opinion. Does the function get called or not?
It doesn't get called - Alexandrescu uses this trick in one of his
books, and it seems fairly common in TMP.
Your way is more elegant though.
Thanks. My questions (which you only quoted one of) in the context
which I posted them were rhetorical by nature, though. They were
questions which may be posed when reading such code.

--
Joel Yliluoma - http://iki.fi/bisqwit/
Nov 29 '07 #5

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

Similar topics

1
2322
by: lauren quantrell | last post by:
Admitedly a very open-ended question but here goes ... Why so may "Connection failure" error messages when my Access 2K users are executing stored procedures that contain UPDATE or INSERT statements? I am using currentconnection before calling the the execute statement and users are using Access built-in login to the SQL Server backend using Windows NT authentication.
0
1692
by: Prasana | last post by:
Hi EveryBody, I have one Problem When I am Creating the DNS Zone Using Resource Record using System Management. Iam Getting an Error Called "Generic Error". Iam Able to Create a Primary Zone File Using CreateZone Class. But How to create other files like SOA, MX, Reverse look Up & other files. Iam working on Win 2000 M/c.Kindly Help. -- Regards, Prasana Srinivasan Software Engineer
3
1637
by: Nate | last post by:
Hello, I have an ASP .Net Web Application running fine on 1 Web server. I cannot seem to get a copy of it to run on a 2nd Web server. I receive the error "Connection Failure" when I try to run the copy on the 2nd Web server with no additional information. I can get a "hello world" page (test.aspx) to run fine on both Web Servers. Are there steps that I need to take beyond simply copying all of the VS.Net2003 Project/Application files...
2
8446
by: jdanoz | last post by:
Hello, i have a vb.net project with a reference to an ActiveX object (ocx). If i try to use the ocx from vb6 project (adding the reference) it works ok (using CreateObject). In vb.net, the CreateObject works ok, the object gets initialized...
10
3738
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server Error in '/QuickStartv20' Application. -------------------------------------------------------------------------------- Configuration Error Description: An error occurred during the processing of a configuration file
0
1334
by: joef | last post by:
I'm running VS.net on my PC (W2K) connected to a remote IIS server (W2K) SP4 that is not a domain controller. I've got the ASPNET and IWAM accounts in the "Impersonate a client after authentication" user rights on the server. I'm in the local admins and debuggers groups. When trying to debug, I'm getting the: Error while trying to run project: Unable to start debugging on the web server. Catastrophic failure
3
57670
by: HoustonComputerGuy | last post by:
I am working on getting my web applications moved to .Net 2.0 and am having some problems with System.Net.Mail. I get the following error when sending the mail: System.Net.Mail.SmtpException was unhandled by user code Message="Failure sending mail." Source="System" StackTrace: at System.Net.Mail.SmtpClient.Send(MailMessage message) at System.Net.Mail.SmtpClient.Send(String from, String
2
18090
by: Radu | last post by:
Hi. I have created a service which I needed to install. Therefore I use InstallUtil. On my dev machine at home I login as Administrator and I have *NO* password set. In my first attempts with InstallUtil I tried with username = Administrator and I left the password textboxes blank. No go - it said An exception occurred during the Install phase. System.ComponentModel.Win32Exception: No mapping between account names and security IDs was...
3
5270
by: =?Utf-8?B?Sm9l?= | last post by:
I know that I have posted this question before, but it is still unresolved and I don't know where to turn to next. I have code that is creating a user (works fine), then sets the account flags (works fine) and then sets the password (fails). Here is the pertinent information: Ex.InnerException.Message: Logon failure: unknown user name or bad password. Ex.Message: Exception has been thrown by the target of an invocation.
0
9646
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
9483
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,...
0
10346
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10096
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
9956
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
6742
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();...
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2887
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.