last day (14 days later) » 

7:56 AM
0
Q: gcc: “inline function used but never defined” in connection with ADL and template trickery

MartinI was playing around with constexpr counters in C++ (based on this), but there's a warning emitted by GCC that I can't get rid of. Below is a fairly minimal source demonstrating the problem. Clang emits no warning, GCC emits two (see below), one I can get rid of easily, but I didn't clutter the c...

 
Maybe you should show how you'd get rid of the first warning, or even supply the code in a format that doesn't generate that warning at all?
"Don't tell me about problems I need to know about" is one way of getting rid of the warning, yes. Which version of GCC are you using?
 
@JonathanLeffler I've tried 5.4.1-2ubuntu1~14.04, 4.8.5-2ubuntu1~14.04.1 and 6.2.0-3ubuntu11~14.04
 
I observe that G++ 6.2.0 produces the warning about -Wnon-template-friend with no other compilation warning flags set (g++ -c -std=c++11 question.cpp) — testing on macOS Sierra 10.12.1 (but I think I compiled GCC on El Capitan). Apple's clang (Apple LLVM version 8.0.0 (clang-800.0.42.1)) doesn't object.
 
@JonathanLeffler the other method that I've tried, works, but sometimes crashes gcc (all mentioned versions), is to make the function templated with a templated type parameter, and calling it with an int constant, but as I said, that's a bug report on my todo list.
@JonathanLeffler how do you think your 6.2.0 differs from my 6.2.0? did you try it with any other version of gcc? ... I've not had any warnings with clang 3.9.
 
Mainly my version was compiled on Mac OS X Yosemite (and is running on macOS Sierra) whereas yours is running on Ubuntu 14.04 LTS. Otherwise, not a lot of difference. Despite the 'used but never defined' warnings, the code linked OK, so something is confused. Given that clang accepts it without a witter (well, clang -O3 -g -std=c++11 -Wall -Wextra -Werror ce13.cpp -o ce13 generates ce13.cpp:24:30: error: unused parameter 'Q' [-Werror,-Wunused-parameter]static constexpr int set(int Q = setter<N+0>::v) {, but if I drop the warnings, it links OK), it is likely gcc that is confused.
 
7:56 AM
Anyway, I notice that clang 3.8 is failed to compile the code in your linked article while clang 3.7 can compile successful with a lot of warning.
 
enabled by default means you cannot switch it off with pragmas or flags.
 
@n.m. but that doesn't necessarily mean the code couldn't be modified somehow to not irritate gcc.
@Danh you're compiling the wrong code, look at this instead
 
@Martin I meant the original code in the linked article, not this one. Since your work is based on it
 
@Danh It does compile if you remove the static_assert, meaning it works differently in that version, which means either it, or the other version is wrong. .... Incidentally it produces many "inline function 'adl_flag' is not defined" warnings.
 
@Martin Does it mean it is not reliable method? I guess it's some edge case of undefined behavior
 
7:56 AM
@n.m. "It can" -> does it? in THIS case? ... specifically, this code can't be modified to avoid the warning? Seriously, I don't know, do you? ... "don't befriend a non-existing function!" -> agreed, ditto, am I using something that is a non-existing function? same here, I don't know, please tell me if you know if this is me using something that just happens to work in a buggy compiler (some versions of gcc and clang) or is it something that should work but doesn't because the other group of compilers is buggy.
@Danh it's definitely an edge case, with a small-ish change I can crash gcc, but that doesn't prove the code is undefined behavior (by the standard's definition), just that compilers are very complex, the C++ standard is very very complex so bugs will happen. .... which is it?
 
temp.friend#4 When a function is defined in a friend function declaration in a class template, the function is instantiated when the function is odr-used ([basic.def.odr]). The same restrictions on multiple declarations and definitions that apply to non-template function declarations and definitions also apply to these implicit definitions.
Because setter<0> is never instantiated, fn(tag<0>) is not instantiated, that said, tag<0> befriend with a non-existing function
 
@Danh doesn't that mean the template deduction fails and the is_set() returning false will be selected? seems to be what some compiler versions do, and what the article's method relies on.
@Danh in which case I don't think "When a function is defined in a friend function declaration" applies, that seems to apply to setter. tag only has a declaration. What does the standard say about a non-existent declared friend in regards to template resolution? ... Sorry I have no idea how to look it up, to me it's all arcane.
@Danh or rather, about constexpr functions that are declared, but not defined in regards to template resolution. constexpr must be inline to be constexpr, but I see your point, a declaration promises, is this specified in the standard?
 
I was wrong the last point, it isn't declared in tag<N>
 
"am I using something that is a non-existing function?" Apparently you do. friend constexpr int fn(tag<N>); is a declaration of a non-template function, but friend constexpr int fn(tag<N>) { return v; } is a definition of a function template. Or so the compilers think.
 
@n.m. oh, sorry, I misunderstood you, I thought "don't befriend a non-existing function" is some kind of clever quote about not using features that don't exist. My bad, funny, but my bad. .... would it be better if it was a template function? Something like the code that crashes gcc?
 
8:22 AM
@n.m. does the standard require the friend function to exist when templates are involved? Can't this be used in template resolution, like SFINAE ? What restrictions aplpy when the friend function is templated, template <...> friend constexpr int fn(tag<T>....)?
 
Sorry I was mistaken, the compilers do not interpret fn declared in setter as a template.
Or do they? Let me check again
 
no, fn in setter is a non-template constexpr function available via ADL as fn(tag<T>), AFAIK what setter accomplishes is the creation of this function definition and making it available to ADL. Each instantiation of setter "inserts" a function [int fn(tag<T>)] into the ADL lookup universe.
 
No they really don't, it was a glitch in the matrix
 
8:38 AM
please explain, that's how I understood it to work.
 
8:50 AM
#include <stdio.h>
class dec {
friend int get(dec);
};
class tem {
friend int get(dec) {
return 1;
}
};
int main () {
printf ("%d\n",get(dec()));
}
that compiles and works
 
I mean they are not templates. according to the standard, and they are not treated as templates by the compiler. It was my mistake to claim that they are.
 
how does that change the situation regarding my question?
 
9:06 AM
Danh's comment about temp.friend#4 applies. Anyway, as far as the standard goes, this entire discussion is mostly irrelevant. An implementation may emit diagnostic messages about valid code, so it is not enough to make the code standard-compliant, you need to satisfy each particular implementation.
 
1) Why would temp.friend#4 apply? That's supposed to be about templated friends, this one isn't.
2) "so it is not enough to make the code standard-compliant, you need to satisfy each particular implementation", that sounds absurd to me, you could be right, but if you are the world is an absurd place, the reason to have a standard is so I don't need to satisfy everyone as long as I satisfy THE standard.
1.5) "An implementation may emit diagnostic messages about valid code", yes, but .... I'll need to think about this, I'm so tired I'm not even sure what I want to say, but it bothers me, a warning is a prompting for corrective action, why warn of nothing?
1.5) yes, as long as you mean something like "if (a=3) {...}" is valid, but you probably meant to compare and not assign, and you know how to fix. If you mean to assign you can do "a=3;if (a)" or "if((a=3))", but you know what you must do to make the world a tranquil place.
 
 
2 hours later…
11:31 AM
> That's supposed to be about templated friends
??? why?
> but you know what you must do
But I have no idea what I must do in this case. The code is totally obscure as is. What does it attempt to do? Detect if a declared function is defined in this translation unit?
 
11:45 AM
I think the standard is deficient in this area anyway, and I feel it should explicitly forbid such tricks.
 
 
3 hours later…
2:32 PM
@n.m. I believe(d) temp.friend is all about templated friends because it's a subchapter of templates and if you notice the examples are ALL exclusively about templated friends. Maybe I need a law degree to interpret this, which (the language of the std) IMHO is the source of a large portion of confusion. Maybe I'm dead wrong. In which case, can you translate that to plain english to me, I've failed to read it.
Here is what the code is supposed to do. tag<T> declares (promises) a function (fn), this function is used in conjunction with ADL in template deduction/resolution/substitution (whatever the right word is) to select between two is_set() function, all based on whether fn is actually brought into existence (odr+template+friend) by instantiating setter<N>, which brings a fn(tag<N>) into existence when instantiated.
Sometimes this must be helped along by constexpr members or function calls or by arithmetic, I don't understand what rules this works by, but I've observed that e.g. reference to setter<T>::v is somehow more powerful in instantiating a full setter {}, and that a reference to an evaluation of a template parameter does more instantiation than without the arithmetic. I don't know why. But I digress.
My so called "knowledge" (double sarcasm intended) is based on the article, guesswork and observation, the standard is unreadable to me, arcane and often seemingly unrelated paragraphs affect it. Maybe it's just me, but really remembering all the detailed implications of all the standard is beyond me. And now you suggest it's too short by the chapter forbidding this? Oh well.
Anyway, "template<int N>
static constexpr int set(int Q = setter<N+0>::v) {
return fn(tag<N>{});
}" uses the constexpness of v and Q=setter<N>::v to bring setter to life, N+0 to use a bit more force (not sure when this is needed) and to top it off calls fn to nudge it even more to existence, is that too much force, I don't know. I'm superstitious because I don't know.
"template <int N>
static constexpr bool is_set(float,tag<N>) {
return false;
}" is the fallback for is_set(1,tag<N>), returns false, "supposed" to be the one selected by the template deduction if set<N> was not invoked before this point in compilation, I use quotes in supposed, because the article says and I've observed, the standard .... as I said, I don't know what the standard says
"template <int N, int Z=fn(tag<N+0>{})>
static constexpr bool is_set(int ,tag<N>) {
return true;
}" is the one selected when fn(tag<N>) was instantiated through setter<N> and/or setter<N>::v, because in this case fn(tag<N>) is really found. Not just a promise of fn(tag<N>). A friend "seems" to be something a bit less like extern, more like a "if there it is a friend", is this what the standard intends? no idea, versions of both gcc and clang have a warning for inline declared but not defined, so maybe
Is this supposed to work? no clue, is the standard complete? no clue, please enlighten me
 
3:01 PM
I think I understand how this code works (or supposed to). I don't understand what kind of problem it is supposed to solve. Why write it in the first place?
> temp.friend is all about templated friends
The standard is usually very careful about the distinction between functions and function templates. If it says "function', it probably means specifically a function, not a function template.
 
3:18 PM
Why write it? It allows you to write compile time constexpr counters for example, why use that? Because sometimes you need a monotonically increasing counter with a context. ...... class x { type_helper<constexpr_counter::next(), int> var; } is vaguely one way want to I use it, what it can do is allow "var" to know it's the first element or the second.
 
4:08 PM
@n.m. can you imagine no useful use for a constexpr counter? I have a constexpr counter which allows for a tag, e.g. constexpr_counter<my_class>::next(), it worked when I wrote it in all compilers I had, except for the warning I asked about, I had used it in maintaining efficient meta data for collection-like types, something like a json adapter, maintain easy cross reference to member data and fast enumeration. C++ doesn't have reflection, I wanted the burden of extra code to be in the library
 
 
2 hours later…
6:19 PM
I can imagine a very restricted use of it, as it can only be unique within a translation unit. Anyway, post your real code and I will try to get rid of the warnings.
 

last day (14 days later) »