473,670 Members | 2,623 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

no ADL / Koenig lookup for function calls with template parameters

cgv
Hi,

I want to distinguish between an int parameter to a template function
depending on whether its value is known at compile time or not. For the
compile time version I want to call the function with the syntax

func<5>();

and for the dynamic version with

func(5);

This can be simply achieved by function overloading through the two function
declarations:

template <int ivoid func() { ... }
void func(int i) {... }

The same thing I want to combine with argument dependent lookup (ADL).
Therefore the functions are declared inside a namespace for example "test"
and both get an argument of a templated typename T:

namespace test {
template <int i, typename Tvoid func(const T&) {...}
template <typename Tvoid func(int i, const T&) {...}
}

If we now declare a struct A inside the same namespace:

namespace test {
struct A { ... };
}

ADL should allow us to use func on an instance of A in the main function
without specifying the namespace test:

void main(int,char** ) {
test::A a;
func<2>(a); // error under visual studio 8 (.Net 2005)
func(2,a); // this is fine
}

In this code the ADL does not work for the compile time version, where the
int is passed as template argument. Now my questions:

1. Is this a bug of the Visual Studio 8 compiler or is this use of ADL not
intended by the standard?

2. Has anyone a good idea of circumvent this problem without messing up the
notation too much?

In order to let you try this out on other compilers here is a complete
example:

#include <iostream>

namespace test

{

template <int i, typename T>

void func_a(const T& t)

{

std::cout << "called func with t = " << t << " and i = " << i <<
std::endl;

}

template <typename T>

void func_b(int i, const T& t)

{

std::cout << "called func with t = " << t << " and i = " << i <<
std::endl;

}

struct A

{

int i;

A(int _i) : i(_i) {}

friend std::ostream& operator << (std::ostream& os, const A& a)

{

return os << a.i;

}

};

}

void main(int, char**)

{

test::A a(3);

func_a<1>(a); // error

func_a<1,test:: A>(a); // error

test::func_a<1> (a); // this is fine but does not help

func_b(1, a); // this works perfect

}

Oct 14 '06 #1
2 2043
cgv wrote:
1. Is this a bug of the Visual Studio 8 compiler or is this use of ADL not
intended by the standard?
VC8 is correct. I would say that rather than not intended its a
problem.
The compiler doesnt know what func is outside its namespace , so it
might be a variable as it parses it e.g:

func < 2

etc.
2. Has anyone a good idea of circumvent this problem without messing up the
notation too much?
One option is to provide a type:
#include <iostream>
namespace my{

template <int Nstruct dummy_int{};

}

//Now use :
namespace my{

template <int N>
void func( dummy_int<N>)
{
std::cout << " N = " << N << '\n';
}

}

int main()
{
func(my::dummy_ int<2>());
func(my::dummy_ int<1000>());
}

regards
Andy Little

Oct 14 '06 #2
cgv schrieb:
In order to let you try this out on other compilers here is a complete
example:
[...]
>
void main(int, char**)
int main (int, char**)

[...]
Oct 14 '06 #3

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

Similar topics

6
3347
by: Patrick Kowalzick | last post by:
Dear all, I have a question about default template parameters. I want to have a second template parameter which as a default parameter, but depends on the first one (see below). Is something like that possible? Some workaround? Thank you, Patrick
1
1608
by: Dave | last post by:
#include <iostream> using std::cout; using std::endl; // #define CAUSE_ERROR namespace N { struct foo_t {};
11
1701
by: REH | last post by:
I'm a little confused about argument dependent lookup. Exactly when does this apply? Specifically, I was hoping to use it to access enumeration constants. For example: namespace Flags { enum flag_type {F1, F2, F3}; } void foo(Flags::flag_type f) {
2
1926
by: Jean-Louis Leroy | last post by:
I'm trying to compile the following code with gcc (version below). I would expect the compile to try the operator ==() declared in Tangram::Backend because of Koenig lookup. But it doesn't happen. Is it me or the compiler? namespace Tangram { namespace Backend { struct RELATIONAL;
3
1222
by: REH | last post by:
I'm a little confused about argument dependent lookup. Exactly when does this apply? Specifically, I was hoping to use it to access enumeration constants. For example: namespace Flags { enum flag_type {F1, F2, F3}; } void foo(Flags::flag_type f) {
5
2341
by: Javier Estrada | last post by:
I'm using a user-defined diagnostics stream and custom manipulators that work with it template<class charT, class traits = std::char_traits<charT> class basic_diagsstream : public std::basic_ostream<charT, traits ... } and a manipulator
1
1286
by: mathieu | last post by:
Hello, I was playing with template programing and I am struggling with the following (*): float f3 = foo((TypeToType<0>::Type*)0); where foo is declared as: template <int T>
12
1869
by: stefan.bruckner | last post by:
Hi, I am looking for a way to achieve the following. I've tried a couple of things, but they all ended up being too complicated: I have a templated class A. I want another class B to be able to call a method defined in A's base class which at runtime determines the template parameters (I know ahead what is allowed) and calls a templated member function B with A's template parameters.
16
1647
by: Juha Nieminen | last post by:
The so-called koenig lookup allows doing odd things like this: #include <algorithm> #include <string> int main() { std::string table; sort(table, table+10); }
0
8468
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
8901
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
8814
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...
0
7415
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
6213
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
5683
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();...
0
4390
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2041
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1792
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.