473,387 Members | 1,407 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Question about "substitution 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 1572
On Nov 29, 2:38 am, Aries Sun <sun.ar...@gmail.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<typename T>
struct is_pointer: public false_type
{
};

template<typename 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<typename T>
void handle_something_do(const T& t, false_type); // for non-pointer cases
template<typename T>
void handle_something_do(const T& t, true_type); // for pointer cases

void handle_something(const T& t)
{
handle_something_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.fiwrote:
>
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.fiwrote:
>>
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
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...
0
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...
3
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...
2
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...
10
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...
0
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...
3
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...
2
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...
3
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
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...
0
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,...
0
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...
0
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...

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.