相关疑难解决方法(0)

在clang中显式指定的参数无效但在gcc中成功编译 - 谁错了?

以下代码在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)

c++ gcc templates clang

8
推荐指数
1
解决办法
5066
查看次数

在没有constexpr的情况下在模板非类型参数处键入转换

请考虑以下代码:

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则不接受,产生以下消息:

/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 …
Run Code Online (Sandbox Code Playgroud)

c++ gcc templates clang c++14

5
推荐指数
1
解决办法
419
查看次数

标签 统计

c++ ×2

clang ×2

gcc ×2

templates ×2

c++14 ×1