Hi NG,
I am trying to get the attached piece of code to work, but I can't
figure out what I'm doing wrong. To me it seems that when I don't pass
an argument to x::do_something , it should use the default value, which
is always_true().
but gcc says: no matching function for call to `x::do_somethin g()'
and msvs says: could not deduce template argument for '_Tp'
So I see what the problem is, but I don't understand it.
Is it possible what I'm trying to do or not?
Thanks,
Mark
#include <iostream>
using namespace std;
struct always_true {
bool operator()() const { return true; }
};
struct always_false {
bool operator()() const { return false; }
};
class x
{
public:
template<typena me _Tp>
bool do_something(co nst _Tp & op = always_true())
{
return op();
}
//bool do_something() { return false; }
};
int main()
{
x classX;
bool rc = classX.do_somet hing(always_tru e()); /*OK*/
cout << (rc ? "TRUE" : "FALSE") << endl;
rc = classX.do_somet hing(); /*compile error*/
cout << (rc ? "TRUE" : "FALSE") << endl;
return 0;
} 3 2747
Capstar wrote: I am trying to get the attached piece of code to work, but I can't figure out what I'm doing wrong. To me it seems that when I don't pass an argument to x::do_something , it should use the default value, which is always_true().
but gcc says: no matching function for call to `x::do_somethin g()' and msvs says: could not deduce template argument for '_Tp'
So I see what the problem is, but I don't understand it.
What exactly do you not understand? The compiler sees "()" in the call.
It tries to find a function that takes no arguments. To determine the
set of _viable_ functions, it needs the full function type known. You
have the only one there: the template. In order to make this a viable
function, it needs to determine the template argument. It has nothing
between the parentheses to determine the '_Tp'. And the default argument
has no bearing on the process of deduction of the template argument.
Is it possible what I'm trying to do or not?
No, not really. You could use a proxy class template for which you can
provide the default template argument, but it's probably more work than
you want.
Just overload your function. Thanks, Mark
#include <iostream> using namespace std;
struct always_true { bool operator()() const { return true; } };
struct always_false { bool operator()() const { return false; } };
class x { public: template<typena me _Tp> bool do_something(co nst _Tp & op = always_true())
Drop the default argument here.
{ return op(); } //bool do_something() { return false; }
Uncomment this and change the definition to { return always_true()() ; }
It will do exactly the same thing as before (and not necessarily the same
as 'return true', since the call will be forwarded to 'always_true's
function call operator).
};
int main() { x classX;
bool rc = classX.do_somet hing(always_tru e()); /*OK*/ cout << (rc ? "TRUE" : "FALSE") << endl;
rc = classX.do_somet hing(); /*compile error*/ cout << (rc ? "TRUE" : "FALSE") << endl;
return 0; }
V
"Capstar" <ne**@ge.homeip .net> wrote in message
news:d9******** **@domitilla.ai oe.org... So I see what the problem is, but I don't understand it. Is it possible what I'm trying to do or not?
Also see this passage in 14.8.2.4 Deducing template arguments from a type
17A template type-parameter cannot be deduced from the type of a func-
tion default argument. [Example:
template <class T> void f(T = 5, T = 7);
void g()
{
f(1); // OK: call f<int>(1,7)
f(); // error: cannot deduce T
f<int>(); // OK: call f<int>(5,7)
}
--end example]
--
Regards,
Ferdi Smit
smit xs4all nl
Ferdi Smit wrote: "Capstar" <ne**@ge.homeip .net> wrote in message news:d9******** **@domitilla.ai oe.org...
So I see what the problem is, but I don't understand it. Is it possible what I'm trying to do or not? Also see this passage in 14.8.2.4 Deducing template arguments from a type
17A template type-parameter cannot be deduced from the type of a func- tion default argument. [Example: template <class T> void f(T = 5, T = 7); void g() { f(1); // OK: call f<int>(1,7) f(); // error: cannot deduce T f<int>(); // OK: call f<int>(5,7) } --end example]
Thanks to both of you. The Victors solution work fine for me.
Mark This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kevin Dean |
last post by:
I'm trying to create an XSL transformation that will strip out
development-specific attributes from deployment descriptors and other XML
files. I have already successfully done so with web.xml but I'm at a
complete loss as to what is wrong with the one below. This is a very
abbreviated server-config.wsdd:
<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns="http://xml.apache.org/axis/wsdd/"...
|
by: CoolPint |
last post by:
Can anyone explain how I can make the following function accept an
default arguement for the last parameter, which should be an optional
functor?
template <typename T, typename FUNCTOR>
void bsort(T * si, T * ei, FUNCTOR cmpfunc)
{
int k = 0;
for (T * i = si; i < ei - 1; i++, k++)
for (T * j = si; j < (ei-k-1) ; j++)
|
by: aaragon |
last post by:
Hello all. I have a simple question that seems trivial but I can't
make it to work. I have a class that takes as a template argument,
another class. The idea is as follows:
#include <iostream>
using namespace std;
template <class ClassB>
class ClassA
|
by: krishnaroskin |
last post by:
Hey all,
I've been running into a problem with default values to template'd
functions. I've boiled down my problem to this simple example code:
#include<iostream>
using namespace std;
// function object
|
by: StephQ |
last post by:
This is from a thread that I posted on another forum some days ago.
I didn't get any response, so I'm proposing it in this ng in hope of
better luck :)
The standard explanation is that pointer to functions are hard to
inline for the compiler, and so you won't be able to avoid function
call overhead.
This is an important aspect when you are calling a function very
frequently for evaluation reason: think of the problem of performing...
| |
by: Wayne Shu |
last post by:
Hi, guys
I am reading Vandevoorde and Josuttis 's "C++ Template The Complete
Guide" these days.
When I read the chapter 15: Traits and Policy classes.
I copy the code in 15.2.2 that use to determining the class type.
The code is below:
|
by: David Sanders |
last post by:
Hi,
I have a class with an integer template parameter, taking values 1, 2
or 3, and a function 'calc' in that class which performs
calculations. Some calculations need only be performed if the
template parameter is 2 or 3; for efficiency, I do not wish to perform
the calculations if the template parameter is 1.
I currently do this as follows:
|
by: William Xu |
last post by:
Compiling:
template <class T = int>
T foo(const T& t)
{}
int main(int argc, char *argv)
{}
gcc complains:
|
by: neelsmail |
last post by:
Hi,
I want to give default value as NULL/0 for non-type template
parameter. I using SunStudio on Linux. I have tried following:
#define non_closer ((int(*)(FILE*))0L)
template<class T, int F(FILE*) = non_closer>
but compiler throws error:
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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();...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |