473,796 Members | 2,578 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 1613
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
1694
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
8447
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
3740
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
1335
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
57673
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
18092
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
5271
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
10459
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
10236
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
10182
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
9055
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...
1
7552
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5445
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
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
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.