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

Home Posts Topics Members FAQ

Turning off template parameter deduction

KD
I have a template function and I'm looking for a way to force the
caller to specify the template parameters. In other words, I would
like to turn off template parameter deduction. For example,

template <class T>
void foo(T t)
{
...
}

foo(123); // I want this to fail.
foo<int>(123); // I want this to succeed.

I was hoping that the explicit keyword would work, but it didn't. So
far, the only half working hack that I came up with is having two
parameters S and T, where S is not used so it must be specified. In
the method, I then do static checks that T is convertible to S, but it
doesn't work in all cases and it adds some complexity to the code.

Any ideas?
Jun 27 '08 #1
4 2542
KD wrote:
I have a template function and I'm looking for a way to force the
caller to specify the template parameters. In other words, I would
like to turn off template parameter deduction. For example,

template <class T>
void foo(T t)
{
...
}

foo(123); // I want this to fail.
foo<int>(123); // I want this to succeed.

I was hoping that the explicit keyword would work, but it didn't. So
far, the only half working hack that I came up with is having two
parameters S and T, where S is not used so it must be specified. In
the method, I then do static checks that T is convertible to S, but it
doesn't work in all cases and it adds some complexity to the code.

Any ideas?
the more important question to ask is: why would you want to do
something like that?

Although I believe there are ways to do what you wanted but I don't
think there is a generic solution.

F
Jun 27 '08 #2
On Jun 19, 3:51*pm, Fei Liu <fei....@gmail. comwrote:
KD wrote:
I have a template function and I'm looking for a way to force the
caller to specify the template parameters. *In other words, I would
like to turn off template parameter deduction. *For example,
template <class T>
void foo(T t)
{
* * ...
}
foo(123); *// I want this to fail.
foo<int>(123); // I want this to succeed.
I was hoping that the explicit keyword would work, but it didn't. *So
far, the only half working hack that I came up with is having two
parameters S and T, where S is not used so it must be specified. *In
the method, I then do static checks that T is convertible to S, but it
doesn't work in all cases and it adds some complexity to the code.
Any ideas?

the more important question to ask is: why would you want to do
something like that?
To implement something similar to standard C++ casts for example.
>
Although I believe there are ways to do what you wanted but I don't
think there is a generic solution.
Sure that there is: just put T in a non deducible context:

template<typena me Tstruct identity { typedef T type; };

template<typena me T>
void do_not_deduce(t ypename identity<T>::ty pe x);

HTH,

--
gpd

Jun 27 '08 #3
KD
On Jun 19, 11:08 am, gpderetta <gpdere...@gmai l.comwrote:
On Jun 19, 3:51 pm, Fei Liu <fei....@gmail. comwrote:
KD wrote:
I have a template function and I'm looking for a way to force the
caller to specify the template parameters. In other words, I would
like to turn off template parameter deduction. For example,
template <class T>
void foo(T t)
{
...
}
foo(123); // I want this to fail.
foo<int>(123); // I want this to succeed.
I was hoping that the explicit keyword would work, but it didn't. So
far, the only half working hack that I came up with is having two
parameters S and T, where S is not used so it must be specified. In
the method, I then do static checks that T is convertible to S, but it
doesn't work in all cases and it adds some complexity to the code.
Any ideas?
the more important question to ask is: why would you want to do
something like that?

To implement something similar to standard C++ casts for example.
Although I believe there are ways to do what you wanted but I don't
think there is a generic solution.

Sure that there is: just put T in a non deducible context:

template<typena me Tstruct identity { typedef T type; };

template<typena me T>
void do_not_deduce(t ypename identity<T>::ty pe x);

HTH,

--
gpd
Thank you. This seems to work for me, and is cleaner than my previous
solution.
Jun 27 '08 #4
On Jun 19, 10:08 pm, gpderetta <gpdere...@gmai l.comwrote:
On Jun 19, 3:51 pm, Fei Liu <fei....@gmail. comwrote:
KD wrote:
I have a template function and I'm looking for a way to force the
caller to specify the template parameters. In other words, I would
like to turn off template parameter deduction. For example,
template <class T>
void foo(T t)
{
...
}
foo(123); // I want this to fail.
foo<int>(123); // I want this to succeed.
I was hoping that the explicit keyword would work, but it didn't. So
far, the only half working hack that I came up with is having two
parameters S and T, where S is not used so it must be specified. In
the method, I then do static checks that T is convertible to S, but it
doesn't work in all cases and it adds some complexity to the code.
Any ideas?
the more important question to ask is: why would you want to do
something like that?

To implement something similar to standard C++ casts for example.
IMO, to implement cast like things doesn't need the trick.

template <typename T, typename ST& user_cast(S& s);

T is result type, can't be deduced yet.
>
Although I believe there are ways to do what you wanted but I don't
think there is a generic solution.

Sure that there is: just put T in a non deducible context:

template<typena me Tstruct identity { typedef T type; };

template<typena me T>
void do_not_deduce(t ypename identity<T>::ty pe x);
aha , impressive trick.
HTH,

--
gpd
Jun 27 '08 #5

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

Similar topics

3
2208
by: Chris Johnson | last post by:
Greetings all: I come across an interesting question (to me anyway) and I do not know the answer. Code/Questions follow: #include <iostream> #if 0 // uncommenting *should* make call ambigous because of // odering deduction of the function signature,
3
1932
by: BigMan | last post by:
Here is a piece of code: #include <memory> using namespace std; template< typename SomeType > void f(auto_ptr_ref< SomeType >) { }
4
2580
by: Mat DeLong | last post by:
I have never been stuck on programming something before to the point I give up... this is a first. I am programming what should be something very easy in C++... using Templates. Here is the code, and the error I get: -------------- namespace LIST { template <typename T> struct Link; template <typename T> class List;
14
2898
by: Bart Samwel | last post by:
Hi everybody, I would really like some help explaining this apparent discrepancy, because I really don't get it. Here is the snippet: void foo(int&); void foo(int const&); template<typename T>
4
2563
by: Neelesh | last post by:
Hi all, I had some confusion about deduction of non-type template parameters for function templates : template <class T, int i> void foo (T, int p = i) ; void bar() { foo<int, 10>(40,40); // OK, T = int, i = 10 foo(40,40); // ERROR, T = int but i cannot be deduced.
2
2371
by: coolpint | last post by:
Can anyone kindly provide an explanation as to why the compiler does not "see" the function template in the contrieved code below? I think the argument deduction fails but can't figure out exactly what the problem is. Is it related to the fact that a nested class is involved? #include <iostream> using std::cout;
7
1532
by: Dilip | last post by:
This is just an academic question but is there any particular reason why this does not work? template<typename T> class Foo { public: Foo(T x) : myvar_(x) { } private: T myvar_;
0
1243
by: Fei Liu | last post by:
Hello, We all know that a template function can automatically deduce its parameter type and instantiate, e.g. template <tpyename T> void func(T a); func(0.f); This will cause func<floatto be instantiated. The user does not have
3
3580
by: Fei Liu | last post by:
Hello, We all know that a template function can automatically deduce its parameter type and instantiate, e.g. template <tpyename T> void func(T a); func(0.f); This will cause func<floatto be instantiated. The user does not have
0
8761
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
9426
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
9281
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
9200
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
9142
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...
1
6722
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
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
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
2680
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.