क्लैंग ++ 3.7.0 के तहत कोड संकलितों का निम्न भाग g ++ 5.3.1 द्वारा अस्वीकार किया गया है। दोनों में -std=c++14
विकल्प है। कौन सा कंपाइलर सही है? कोई भी जानता है कि इस बारे में मानक वार्ता में कहां है? धन्यवाद।constexpr फ़ंक्शन में फेंक
#include <stdexcept>
using namespace std;
constexpr int f(int n) {
if (n <= 0) throw runtime_error("");
return 1;
}
int main() {
char k[f(1)];
}
आउटपुट
[hidden] g++ -std=c++14 c.cpp
c.cpp: In function ‘constexpr int f(int)’:
c.cpp:7:1: error: expression ‘<throw-expression>’ is not a constant-expression
}
^
[hidden] clang++ -std=c++14 c.cpp
[hidden]
[hidden] g++ -v
Using built-in specs.
COLLECT_GCC=/usr/bin/g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/5.3.1/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,objc,obj-c++,fortran,ada,go,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --disable-libgcj --with-isl --enable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 5.3.1 20151207 (Red Hat 5.3.1-2) (GCC)
[hidden]
[hidden] clang++ -v
clang version 3.7.0 (http://llvm.org/git/clang.git 2ddd3734f32e39e793550b282d44fd71736f8d21)
Target: x86_64-unknown-linux-gnu
Thread model: posix
Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/3.4.6
Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/5.3.1
Selected GCC installation: /usr/lib/gcc/x86_64-redhat-linux/5.3.1
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
मानक नहीं करता है:
लेकिन वहाँ एक बेहतर वैकल्पिक हल, अभी भी
c++14
constexpr एक्सटेंशन के सभी को बनाए रखना है यह नहीं कहता कि यह फेंक नहीं सकता है, जहां तक मैं –देख सकता हूं, यह इंगित किया गया था कि इस पर एक जीसीसी बग रिपोर्ट है, कोई काम-आसपास प्रदान नहीं किया गया है। –