标签: clang++

g ++和operator <<在评估期间更改值时会产生无意的结果

这是一些简单的代码.

#include <iostream>
using namespace std;

bool func(char* m)
{
    *m = '4';
    return true;
}

using namespace std;
int main()
{
    char c1 = '3';
    cout  << "a" << c1 << func(&c1) << c1 << "b" << endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当使用g ++ -O0(4.7.2)编译时,输出为a413b,对于-O2,输出为a414b.对于clang ++ 3.2,输出为a314b.

我在这部分代码中为c ++做了什么未定义的事情吗?

c++ g++ clang++

3
推荐指数
2
解决办法
95
查看次数

为什么clang不使用constexpr版本的斐波那契计算斐波那契(500)?

我正在尝试constexpr:

#include <iostream>

constexpr long long fibonacci(const int x)
{
    return x <= 1 ? 1 : fibonacci(x - 1) + fibonacci(x - 2);
}

int main()
{
    const long long lol = fibonacci(500);
    std::cout << lol << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

所以我想lol在编译时计算:

toogy@stewie
» g++ -std=c++14 -g src/test.cc -o test.out
toogy@stewie
» ./test.out 
4859788740867454402
Run Code Online (Sandbox Code Playgroud)

它的工作非常好g++.在编译时,它甚至会进行一些记忆,优化这种糟糕的斐波纳契功能,然后fibonacci(500)立即进行计算.

然后我尝试clang++:

toogy@stewie
» clang++ -std=c++1y -g src/test.cc -o test.out 
toogy@stewie
» ./test.out
... very long
Run Code Online (Sandbox Code Playgroud)

lol …

c++ g++ clang++

3
推荐指数
1
解决办法
410
查看次数

使用bool初始化std :: string

考虑以下初始化:

std::string falseString = false;
std::string trueString = true;
Run Code Online (Sandbox Code Playgroud)

有了g++ 5.2.0,编译器抛出警告falseString,而错误trueString.

随着clang++ 3.6 -std=c++11,编译器会引发错误都falseString还有trueString.

Q1)为什么不同的行为gcc即使两个初始化值都是相同的类型(bool)?

Q2)哪个编译器是正确的,为什么?标准说什么?

编辑:

错误:没有可行的从'bool'转换为'std :: string'(又名'basic_string')

警告:将'false'转换为'std :: __ cxx11 :: basic_string <_CharT,_Traits,_Alloc> :: basic_string(const _CharT*,const _Alloc&)的参数1的指针类型[与_CharT = char; _Traits = std :: char_traits; _Alloc = std :: allocator]'[ - Wconversion-null]

c++ g++ stdstring c++11 clang++

3
推荐指数
1
解决办法
591
查看次数

使类模板强制在继承构造函数中重复基类模板参数

我最近将一个类从模板更改为不,并发现在编写using声明从模板化基类继承构造函数时,我不能再省略模板参数.只要我的课没有模板化,我就可以省略这些论点,只要我不能.在下面的可编译片段中bar表示之前的类并buzz表示之后的类.我测试了GCC 5.2和Clang 3.7,它们具有相同的行为.这是编译器错误还是标准?

#include <iostream>

template<class A, class B>
struct foo {
    foo(int x) {
        std::cout << x << std::endl;
    }
};

struct bar : foo<bar, short> {
    using foo::foo; // this appears legal
    // using foo<bar, short>::foo; // this works too, thought I would need it
};

template<class X>
struct buzz : foo<buzz<X>, short> {
     //using foo::foo; // no longer legal for some reason
    using foo<buzz<X>, short>::foo; // now do actually need this
};

int …
Run Code Online (Sandbox Code Playgroud)

c++ templates g++ clang++ c++14

3
推荐指数
1
解决办法
54
查看次数

将NULL作为对象返回时没有收到任何警告

我不明白为什么我没有收到警告(用g ++或clang ++)来返回NULL作为newtstr()下面的对象:

#include<iostream>
using namespace std;

string newstr();

int main()
{
        string s = newstr();
        cout << s << endl;
        return 0;
}

string newstr()
{
        return NULL;
}
Run Code Online (Sandbox Code Playgroud)

.

$ g++ -Wall -Wextra -pedantic testptr.cpp
$
Run Code Online (Sandbox Code Playgroud)

但是,我得到一个运行时错误:

$ ./a.out
terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_S_construct null not valid
Aborted
$
Run Code Online (Sandbox Code Playgroud)

.

$ g++ --version
g++ (GCC) 4.8.0
Run Code Online (Sandbox Code Playgroud)

c++ warnings g++ compiler-warnings clang++

3
推荐指数
1
解决办法
85
查看次数

clang拒绝模板`/`运算符,但gnu c ++接受它

在科学编程的单元管理环境中,我正在管理以下课程:

template <class UnitName>
class Quantity 
{
  double value;

public:

  Quantity(double val = 0) : value(val) {}

  Quantity(const Quantity &) {}

  Quantity & operator = (const Quantity &) { return *this; }

  double get_value() const noexcept { return value; }

  operator double() const noexcept { return value; }

  template <class SrcUnit>
  Quantity(const Quantity<SrcUnit> &)
  {
    // here the conversion is done
  }

  template <class SrcUnit>
  Quantity & operator = (const Quantity<SrcUnit> &)
  {
    // here the conversion is done
    return …
Run Code Online (Sandbox Code Playgroud)

c++ gcc clang++

3
推荐指数
1
解决办法
74
查看次数

C++评估顺序

我试图弄清楚C++ 11规范中是否有任何内容.以下代码的预期行为(此处GitHub链接):

struct Scalar {
    int data;

    Scalar(int x) : data(x) {}

    int get() {
        return data;
    }

    Scalar &square() {
        scale(data);
        return *this;
    }

    void scale(int rhs) {
        data *= rhs;
    }
};

int main() {
    Scalar v(3);

    v.square().scale(v.get());

    return v.data;
}
Run Code Online (Sandbox Code Playgroud)

这主要是因为发现它在g++和之间做了不同的事情clang++:

$ g++ --version
g++ (GCC) 6.2.1 20160830
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; …
Run Code Online (Sandbox Code Playgroud)

c++ g++ c++11 clang++

3
推荐指数
1
解决办法
114
查看次数

用于std :: thread的scoped线程包装器

我正在尝试制作一个范围的线程.

#include <iostream>
#include <thread>

class ScopedThread {
 public:
    template< class Function, class... Args>
    explicit ScopedThread( int id, Function&& f, Args&&... args)
        : m_thread( std::ref(f), std::forward<Args>(args)...)
        , id(std::move(id)) {
    }



    int getId() const { return id; }

    ~ScopedThread() { m_thread.join(); }
 private:
    std::thread m_thread;
    int id;

};

class Worker {
 public:
    Worker(int id): thd(id, &Worker::work, this) { }

    void work() {
       for( int i = 0; i < 10; i++)
        std::cout << "I am working" << std::endl;
    }


 private:
    ScopedThread …
Run Code Online (Sandbox Code Playgroud)

multithreading move-semantics c++11 clang++ stdthread

3
推荐指数
1
解决办法
282
查看次数

clang std :: isspace编译错误

以下代码在VS 2015(更新3)和gcc 6.3(C++ 14)上编译正常,没有任何问题.

#include <string>
#include <locale>

int main()
{
    std::u16string ustr = u"Android";

    bool var = std::isspace(ustr[0],std::locale());

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,在clang/Xcode上它失败并出现以下错误

Error(s):
source_file.cpp:8:10: warning: unused variable 'var' [-Wunused-variable]
    bool var = std::isspace(ustr[0],std::locale());
         ^
In file included from source_file.cpp:2:
In file included from /usr/include/c++/v1/locale:182:
/usr/include/c++/v1/__locale:705:44: error: implicit instantiation of undefined template 'std::__1::ctype<char16_t>'
    return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c);
                                           ^
source_file.cpp:8:21: note: in instantiation of function template specialization 'std::__1::isspace<char16_t>' requested here
    bool var = std::isspace(ustr[0],std::locale());
                    ^
/usr/include/c++/v1/__locale:427:53: note: template is …
Run Code Online (Sandbox Code Playgroud)

c++ c++11 clang++ c++14

3
推荐指数
2
解决办法
535
查看次数

Can I use C++20 concepts properly in a using declaration?

I was playing around a bit with Concepts offered in C++20 and came up with a simple example, which, to my surprise, does not produce the expected results (please leave any discussion on the usefulness of my example be :-)):

#include <iostream>
#include <type_traits>
#include <vector>

template <typename T>
concept i_am_integral = requires { std::is_integral_v<T> == true; };

template <typename T> requires i_am_integral<T>
using intvector = std::vector<T>;

int main() {
    intvector<int> v = { 1, 2, 3 }; // <- …
Run Code Online (Sandbox Code Playgroud)

c++ clang++ c++-concepts c++20

3
推荐指数
1
解决办法
113
查看次数