小编son*_*yao的帖子

C++模板方法:为什么Node <int>没问题,但Node <float>?

我很少使用模板.我不知道为什么我看到下面的代码生成错误的push方法Node<float>

构建错误是:没有匹配函数来调用push.

Node<int>* push方法很好.

Node<float>* head1 = NULL;
push(&head1, 1);

template <typename T>
struct Node {
    T data;
    Node* next;
};

template <typename T>
void push(Node<T>** head, T data) {
    Node<T>* tmp = *head;
    Node<T>* newNode = NULL; //createNode(data);

    if (tmp == NULL) {
        *head = newNode;
    }
    else {
        while (tmp->next)
            tmp=tmp->next;

        tmp->next = newNode;
    }
}

int main(int argc, const char * argv[]) {
    Node<float>* head1 = NULL;
    push(&head1, 1);

    Node<int>* head = NULL;
    push(&head, …
Run Code Online (Sandbox Code Playgroud)

c++ templates

15
推荐指数
3
解决办法
1538
查看次数

值初始化:MSVC vs clang

#include<cstddef>

template<typename T, std::size_t N>
struct A {
    T m_a[N];
    A() : m_a{} {}
};

struct S {
    explicit S(int i=4) {}
};

int main() {
    A<S, 3> an;
}
Run Code Online (Sandbox Code Playgroud)

上面的代码与MSVC(2017)编译良好,但与clang 3.8.0(输出clang++ --version && clang++ -std=c++14 -Wall -pedantic main.cpp)失败:

clang version 3.8.0 (tags/RELEASE_380/final 263969)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin
main.cpp:6:15: error: chosen constructor is explicit in copy-initialization
    A() : m_a{} {}
              ^
main.cpp:14:13: note: in instantiation of member function 'A<S, 3>::A' requested here
    A<S, 3> an; …
Run Code Online (Sandbox Code Playgroud)

c++ initialization clang visual-studio language-lawyer

15
推荐指数
2
解决办法
962
查看次数

为什么在模板参数上使用 decltype?

https://github.com/stlab/libraries/blob/main/stlab/concurrency/main_executor.hpp中,我读到

struct main_executor_type {
    using result_type = void;

    template <typename F>
    void operator()(F f) const {
        using f_t = decltype(f);

        dispatch_async_f(dispatch_get_main_queue(), new f_t(std::move(f)), [](void* f_) {
            auto f = static_cast<f_t*>(f_);
            (*f)();
            delete f;
        });
    }
};
Run Code Online (Sandbox Code Playgroud)

有什么意义decltype(f),为什么不简单地使用F呢?

c++ templates types decltype

15
推荐指数
1
解决办法
661
查看次数

'char*'的模板参数推导

我有以下功能(仅用于复制问题):

template <typename KeyT>
void func(const KeyT cptr) {
  std::cout << typeid(KeyT).name() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我想用字符串文字来调用它,如下所示:

func<char*>("literal");
Run Code Online (Sandbox Code Playgroud)

但是,我最终得到了警告:

warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wc++11-compat-deprecated-writable-strings]

我有一个特定需要使用char*作为我的主要类型,我期待TAD要考虑param typeconst char*全部,因为我不是引用服用.

警告来自两者clangg++编译器.

如何param type推断这里?

提前致谢.

c++ templates c++11

14
推荐指数
2
解决办法
591
查看次数

为什么我不能使用间接运算符取消引用指向数组元素的对象的指针?

是不是可以使用间接(取消引用)运算符取消引用指向存储在数组中的对象的指针,还是我做错了?

#include <iostream>

class A {
    public:
        virtual void test() {
            std::cout << "A\n";
        }
};

class B : public A {
    public:
        void test() {
            std::cout << "B\n";
        }
};


int main() {
    A* v[2];

    v[0] = new A();
    v[1] = new B();

    v[0]->test();
    *(v[1]).test(); // Error! If the arrow operator is used instead
                    // though, the code compiles without a problem.

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

这是我得到的错误:

$ g++ -std=c++11 test.cpp && ./a.out 
test.cpp: In function ‘int main()’:
test.cpp:26:13: error: …
Run Code Online (Sandbox Code Playgroud)

c++ pointers operators operator-precedence dereference

14
推荐指数
2
解决办法
2190
查看次数

为什么GCC 5.3.0在绑定对"this"指针的引用时发出警告

这是最小的例子:

class A
{
    A* const& this_ref;
public:
    A() : this_ref(this) {}
};
Run Code Online (Sandbox Code Playgroud)

GCC 5.3.0发出警告:

警告:临时绑定到'A :: this_ref'只会持续存在,直到构造函数退出[-Wextra] A():this_ref(this){}

那是this暂时的吗?什么... MSVC 2015对此保持沉默,并且this_ref->member在我的情况下在构造函数之外引用类成员给出了预期的行为(但可能仅仅是UB的情况,不确定).


编辑:

请注意,这个问题扩展了一个链接尽可能重复,因为它不是关于创建此类引用的方法的一般性问题,而是关于警告GCC(以及除MSVC之外的其他可能的编译器)在创建时产生的.

c++ gcc reference this language-lawyer

14
推荐指数
2
解决办法
439
查看次数

std :: thread构造函数第三个模板参数的目的是什么?

这是std::thread构造函数声明的方式(使用Visual Studio 2015):

template<class _Fn,
class... _Args,
class = typename enable_if<
    !is_same<typename decay<_Fn>::type, thread>::value>::type>
explicit thread(_Fn&& _Fx, _Args&&... _Ax)
Run Code Online (Sandbox Code Playgroud)

没有问题就_Fn_Args,然而,第三class = ...混淆了我完全.它做什么,它是如何工作的以及它的用途是什么?

c++ multithreading templates c++11

14
推荐指数
2
解决办法
769
查看次数

为什么当我取消引用数组指针时,结果值是指向数组第一个元素的指针,而不是整个数组对象?

#include<iostream>

int num[3]={66,77,88};

int main()
{
    int(*pi)[3]=&num;
    std::cout<<*pi;
} 
Run Code Online (Sandbox Code Playgroud)

结果是地址而不是数组。这背后的解释是什么?

c++ arrays

14
推荐指数
1
解决办法
1405
查看次数

为什么c ++标准支持函数strftime而不是strptime?

为什么C++标准支持功能strftime()但不支持strptime()strftime()可以将时间更改为字符串,但是没有可以将字符串更改回时间的函数.

在Posix strptime()上可以使用类似C的函数,使用它需要处理混合C和C++代码的潜在问题.

c c++ posix strftime strptime

13
推荐指数
1
解决办法
1561
查看次数

警告:支持指针的对象将在 std::pair 的完整表达式末尾被销毁

以下代码给出了悬空指针错误。

    std::vector<std::pair<std::string, int>> c;
    for (auto& b : c) {
        const auto& [s, i] = b;
        std::string_view v = s.substr(i);
        std::cout << v;
    }
Run Code Online (Sandbox Code Playgroud)

我认为它b保存了对 的引用std::pair<std::string, int>,所以s应该是对pair对象中字符串的引用。为什么这会产生悬空指针错误?我在这里缺少什么?godbolt 链接: https: //godbolt.org/z/4zMvbr

c++ temporary stdstring string-view c++17

13
推荐指数
1
解决办法
1万
查看次数