Skip to content

Instantly share code, notes, and snippets.

@seanbaxter
Created October 5, 2017 14:12
Show Gist options
  • Save seanbaxter/38dd5dbf7128526056927920291f5920 to your computer and use it in GitHub Desktop.
Save seanbaxter/38dd5dbf7128526056927920291f5920 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <type_traits>
#include <algorithm>
#include <utility>
#include <map>
#include <vector>
#include <tuple>
template<typename type_t>
struct foo_t {
int x;
void bar() {
static_assert(!std::is_same<int, decltype(x)>::value, "x");
int y;
static_assert(!std::is_same<int, decltype(y)>::value, "y");
}
};
@seanbaxter
Copy link
Author

seanbaxter commented Oct 5, 2017

The assert for x does NOT go off, because in the function scope, clang/gcc/msvc treat the id-expression x as a type-dependent expression, even though it is a non-dependent member of the current instantiation. This is also why you can do nonsense like this->blahblah. "this" is a type-dependent expression, but the member access is a lookup into the current instantiation, and blahblah is not found, and since there is no dependent base-class, the lookup should fail and the program should be ill-formed.

[temp.dep.expr]/5
A class member access expression (8.2.5) is type-dependent if the expression refers to a member of the current
instantiation and the type of the referenced member is dependent, or the class member access expression
refers to a member of an unknown specialization. [ Note: In an expression of the form x.y or xp->y the
type of the expression is usually the type of the member y of the class of x (or the class pointed to by xp).
However, if x or xp refers to a dependent type that is not the current instantiation, the type of y is always
dependent. If x or xp refers to a non-dependent type or refers to the current instantiation, the type of y is
the type of the class member access expression. — end note ]

g++ -std=c++14 -o test test.cxx -Wall && ./test
test.cxx: In member function ‘void foo_t<type_t>::bar()’:
test.cxx:18:5: error: static assertion failed: y
static_assert(!std::is_same<int, decltype(y)>::value, "y");
^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment