这是我在学习期间发现的:
#include<iostream>
using namespace std;
int dis(char a[1])
{
int length = strlen(a);
char c = a[2];
return length;
}
int main()
{
char b[4] = "abc";
int c = dis(b);
cout << c;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所以在变量中int dis(char a[1]),[1]似乎什么都不做,根本不起作用
,因为我可以使用a[2].就像int a[]或char *a.我知道数组名称是一个指针,以及如何传达一个数组,所以我的谜题不是这个部分.
我想知道的是为什么编译器允许这种行为(int a[1]).或者它有其他我不知道的含义?
我看到Q_NULLPTR在Qt源代码和示例中被大量使用,但是我没有找到关于它究竟是什么以及什么时候应该使用的文档.
例如,在新的Qt v5.6中添加的新Qt SerialBus模块的官方演示中:
if (!m_canDevice->connectDevice()) {
delete m_canDevice;
m_canDevice = Q_NULLPTR;
Run Code Online (Sandbox Code Playgroud)
这是否适用于nullptr在C++ 11中添加之前的目的?如果是这样,现在我们有C++ 11,我应该使用Q_NULLPTR吗?
PS:我尝试在Qt源代码中搜索宏的定义,但未能找到它.
GCC v6.1(结果与v5.1相同)使用flags成功编译了下面的代码-std=c++11 -Wall -Wextra -Wpedantic,但产生了这个警告:
variable templates only available with -std=c++14 or -std=gnu++14
Run Code Online (Sandbox Code Playgroud)
代码:
#include <iostream>
template <typename T>
struct ParamMetadata {
T min;
T max;
};
template <class T1, class T2>
class FooMap {};
template <typename T>
// WARNING PRODUCED ON THIS LINE
extern FooMap<int, ParamMetadata<T> > metadataHashmap;
int main() {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Clang v3.8产生了类似的警告:
variable templates are a C++14 extension [-Wc++14-extensions]
Run Code Online (Sandbox Code Playgroud)
如果这是仅在C++ 14中可用的功能,为什么用C++ 11标记编译就好了,我可以运行可执行文件?这不应该是一个致命的错误吗?
如果我不知道函数将传递多少个参数,我可以使用参数包装来编写函数:
def add(factor, *nums):
"""Add numbers and multiply by factor."""
return sum(nums) * factor
Run Code Online (Sandbox Code Playgroud)
或者,我可以通过传递一个数字列表作为参数来避免参数打包:
def add(factor, nums):
"""Add numbers and multiply by factor.
:type factor: int
:type nums: list of int
"""
return sum(nums) * factor
Run Code Online (Sandbox Code Playgroud)
使用参数打包而*args不是传递数字列表是否有优势?或者是否存在更合适的情况?
我有一个子项目,我将所有QTest单元测试放在一起,并构建一个运行测试的独立测试应用程序(即我在Qt Creator中运行它).我有多个可以执行的测试类qExec().但是我不知道执行多个测试类的正确方法是什么.
目前我这样做(MVCE):
QT -= gui
QT += core \
testlib
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
TARGET = testrunner
HEADERS += test_foo.h
SOURCES += main.cpp
Run Code Online (Sandbox Code Playgroud)
#include <QtTest>
#include <QCoreApplication>
#include "test_foo.h"
int main(int argc, char** argv) {
QCoreApplication app(argc, argv);
TestFooClass testFoo;
TestBarClass testBar;
// NOTE THIS LINE IN PARTICULAR.
return QTest::qExec(&testFoo, argc, argv) || QTest::qExec(&testBar, argc, argv);
}
Run Code Online (Sandbox Code Playgroud)
#include <QtTest>
class TestFooClass: public QObject
{
Q_OBJECT
private slots: …Run Code Online (Sandbox Code Playgroud) 在我的程序中,我通过串行通信工作很多,因此QByteArray经常使用.
我想知道是否有一个更短的方法来初始化QByteArray特定字节比:
const char test_data[] = {
static_cast<char>(0xB1), static_cast<char>(0xB2),
0x5, static_cast<char>(0xFF),
static_cast<char>(0xEE), static_cast<char>(0xEE),
static_cast<char>(0x0)}; // Note QByteArray should be able to hold 0 byte
const QCanBusFrame frame = QCanBusFrame(0xA1, QByteArray(test_data));
Run Code Online (Sandbox Code Playgroud)
这static_cast<char>是必要的,因为否则C++ 11会出现关于缩小的错误,因为0x7F到0xFF的范围大于a char可能适合的范围 - 但是a char是QByteArray构造函数要求的.
这是使用的QByteArray构造函数:
QByteArray::QByteArray(const char *data, int size = -1)
我试图通过遍历枚举创建一副扑克牌Suit和Rank(我知道有没有遍历枚举好办法,但我没有看到另一种).我通过enum_count在每个枚举的末尾添加一个枚举器来做到这一点,其值用于表示枚举的长度和结尾.
#include <vector>
using namespace std;
enum class Suit: int {clubs, diamonds, hearts, spades, enum_count};
enum class Rank: int {one, two, three, four, five, six, seven, eight,
nine, ten, jack, queen, king, ace, enum_count};
struct Card {
Suit suit;
Rank rank;
};
class Deck{
vector<Card> cards{};
public:
Deck();
};
Deck::Deck() {
// ERROR ON THE BELOW LINE
for (Suit suit = Suit::clubs; suit < Suit::enum_count; suit++) {
for (Rank rank = Rank::one; rank < …Run Code Online (Sandbox Code Playgroud) 我正在调查为什么这段代码可以在我的具有 GCC v7.2 的 PC 上编译,但不能使用我们工具链的 GCC v5.4 进行编译,depsite-std=c++14 -Wpedantic -pedantic-errors被传递:
#include <array>
#include <vector>
#include <tuple>
typedef std::tuple<const char *, const char *, bool> StrStrBool;
const std::vector<StrStrBool> cApIDValidTestValues {
{
{"str1", "str2", true },
{ "str3", "str4", false }
}
};
Run Code Online (Sandbox Code Playgroud)
错误是:
<source>:12:1: error: converting to 'std::tuple<const char*, const char*, bool>' from initializer list would use explicit constructor 'constexpr std::tuple< <template-parameter-1-1> >::tuple(_UElements&& ...) [with _UElements = {const char (&)[5], const char (&)[5], bool}; <template-parameter-2-2> = void; _Elements …
我可以用来git log -G search_term显示添加或删除包含字符串的行的提交search_term,但这仅显示提交消息和日期等元数据,而不显示实际的匹配行。
我想要一些输出,例如:
some_hash: filename.c: + if search_term == True
Run Code Online (Sandbox Code Playgroud)
是否可以使用 Git 中的可用选项而无需编写 shell 脚本?
我正在尝试编写一个值参数化测试,其中测试值仅在测试类被实例化后创建,即测试值存储在非静态变量中.这意味着我不能做我通常做的事情,容器是静态的:
INSTANTIATE_TEST_CASE_P(SomeCriteria, SomeTest,
ValuesIn(SomeClass::staticContainerWithTestINputs) );
Run Code Online (Sandbox Code Playgroud)
这是我遇到的一个MVCE示例:
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using namespace testing;
// This is not a test class, so I can't modify `myInt` to be static just so
// that I can write tests.
struct CustomClass
{
int myInt = 0;
};
class Fixture : public ::testing::Test {
protected:
CustomClass myCustomCls;
virtual void SetUp() override
{
// This variable needs to be used in the parameterized test.
myCustomCls.myInt = 42;
}
};
class ValueParamTest : public Fixture, public …Run Code Online (Sandbox Code Playgroud)