以下代码在g ++中编译时没有问题:
#include <iostream>
#include <string>
#include <tuple>
template<typename T>
void test(const T& value)
{
std::tuple<int, double> x;
std::cout << std::get<value>(x);
}
int main() {
test(std::integral_constant<std::size_t,1>());
}
Run Code Online (Sandbox Code Playgroud)
我使用了这个命令:
g++ test.cpp -o test -std=c++14 -pedantic -Wall -Wextra
Run Code Online (Sandbox Code Playgroud)
但是当我切换g++到clang++(使用g ++ 5.1.0和clang ++ 3.6.0)时,我收到以下错误:
test.cpp:9:18: error: no matching function for call to 'get'
std::cout << std::get<value>(x);
^~~~~~~~~~~~~~~
test.cpp:13:5: note: in instantiation of function template specialization 'test<std::integral_constant<unsigned long, 1> >' requested here
test(std::integral_constant<std::size_t,1>());
^~~~~~~~~~~~~~~
<skipped>
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.1.0/../../../../include/c++/5.1.0/tuple:867:5: note: candidate template ignored: …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
struct A {
constexpr operator int() { return 42; }
};
template <int>
void foo() {}
void bar(A a) {
foo<a>();
}
int main() {
foo<A{}>();
const int i = 42;
foo<i>(); // (1)
A a{};
static_assert(i == a, "");
bar(a);
foo<a>(); // error here
}
Run Code Online (Sandbox Code Playgroud)
带有c ++ 14的Clang 3.7接受此,而带有c ++ 14的gcc 5.2.0则不接受,产生以下消息:
Run Code Online (Sandbox Code Playgroud)/tmp/gcc-explorer-compiler1151027-68-1f801jf/example.cpp: In function 'int main()': 26 : error: the value of 'a' is not usable in a constant expression foo<a>(); ^ 23 : note: 'a' was …