我有一个结构
template <typename T>
struct Demo {
T x;
T y;
};
Run Code Online (Sandbox Code Playgroud)
我正在尝试编写一个类似于std::get元组的泛型函数,它接受一个编译时索引I并返回一个左值引用到结构的I-th成员,如果用一个左值DemoStruct<T>和一个对I第-n成员的右值引用调用它如果用rvalue调用它的结构DemoStruct<T>.
我目前的实现看起来像这样
template <size_t I, typename T>
constexpr decltype(auto) struct_get(T&& val) {
auto&& [a, b] = std::forward<T>(val);
if constexpr (I == 0) {
return std::forward<decltype(a)>(a);
} else {
return std::forward<decltype(b)>(b);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这不符合我的预期,并始终返回rvalue-reference T.
这是一个显示问题的wandbox.
返回对结构成员的引用的正确方法是什么,保留传递给函数的结构的值类别?
编辑:正如Kinan Al Sarmini指出的那样,auto&& [a, b] = ...确实推断了非参考类型的类型a和b非参考类型.对于std::tuple例如两者也是如此
std::tuple my_tuple{std::string{"foo"}, std::string{"bar"}}; …Run Code Online (Sandbox Code Playgroud) c++ language-lawyer perfect-forwarding c++17 structured-bindings
我正在使用指向实现的成语实现不可变对象上的装饰器模式.基本上我的设置看起来像这样
struct Object : ObjectBase {
void doSmth() override {
impl->doSmth();
}
// this is the function I'd like to implement
Object decorateWith(std::unique_ptr<ObjectDecorator>&&);
private:
std::unique_ptr<ObjectBase> impl;
};
struct ObjectDecorator : ObjectBase {
void doSmth() override {
// do some stuff
impl->doSmth();
// do some more stuff
}
private:
std::unique_ptr<ObjectBase> impl;
};
Run Code Online (Sandbox Code Playgroud)
这里,decorateWith函数应该具有不同的行为,这取决于它是否是callen on的对象是否是临时的.如果在非临时对象上调用它,它应该返回一个新的Object实例,我必须在其中创建当前Object的深层副本并将其存储在装饰器的unique_ptr中,同时新的Object本身的impl指针指着装饰者.但是,如果在临时调用decorateWith,则只需创建一个ObjectDecorator,只需将当前对象的impl指针移动到装饰器的impl指针中,然后让对象指向新的装饰器即可.
为了阻止我需要一种方法来确定从decorateWith中调用对象是否是临时的,然后根据该检查的结果使用tag-dispatch.那可能吗?
最好的Xodion
编辑:示例调用者代码可能如下所示:
decorateWith是在非临时的上调用的
int main() {
Object x{};
// this call does not modify x so it can be reused later
Object y = x.decorateWith{std::make_unique<ObjectDecorator>()};
y.doSmth(); …Run Code Online (Sandbox Code Playgroud)在他的谈话在CppCon,理查德·史密斯提到,即使模块TS支持是目前正在进行的工作,它已经可以使用.所以我从svn构建了clang 4.0,并在一个非常简单的例子中尝试了它.在我的myclass.cppm文件中,我为一个简单的包装器定义了一个int
module myclass;
export class MyClass {
public:
MyClass (int i)
: _i{i} {}
int get() {
return _i;
}
private:
int _i;
};
Run Code Online (Sandbox Code Playgroud)
我main.cpp刚刚创建类的一个实例,并输出其保持int到std::cout.
#include <iostream>
#include <string>
import myclass;
int main(int, char**) {
MyClass three{3};
std::cout << std::to_string(three.get()) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试通过clang++ -std=c++1z -fmodules-ts main.cpp和编译它,clang++ -std=c++1z -fmodules-ts myclass.cppm main.cpp但这不起作用,我在两种情况下都得到相同的错误消息:
main.cpp:3:8: fatal error: module 'myclass' not found
import test.myclass;
~~~~~~~^~~~
1 error generated. …Run Code Online (Sandbox Code Playgroud) 我刚刚观察到libc ++的一个奇怪问题,当使用SFINAE来检测模板类型是否是默认构造时.
以下是我能够提出的最小例子:
#include <iostream>
#include <type_traits>
template <typename T>
struct Dummy;
template <>
struct Dummy<int>{};
template <typename T, typename = void>
struct has_dummy : std::false_type {};
template <typename T>
struct has_dummy<C, std::enable_if_t<std::is_default_constructible<Dummy<T>>::value>> : std::true_type{};
int main() {
std::cout << std::boolalpha << has_dummy<int>{}() << '\n';
std::cout << std::boolalpha << has_dummy<double>{}() << '\n';
}
Run Code Online (Sandbox Code Playgroud)
它编译并输出预期的行,true并false在使用时使用g ++或clang ++编译libstdc++.但是,当我尝试使用libc ++(即clang++ -stdlib=libc++ -std=c++1z test.cpp)编译它时,我收到以下错误:
/usr/bin/../include/c++/v1/type_traits:2857:38:错误:隐式实例化未定义模板'Dummy':public integral_constant
/usr/bin/../include/c++/v1/type_traits:3166:14:注意:在这里请求模板类'std :: __ 1 :: is_constructible>'的实例化:public is_constructible <_Tp>
test.cpp:14:43:注意:在这里请求模板类'std :: …
我有以下目录结构
.
??? 3rdparty
? ??? liba
? ??? include
? ? ??? liba.hpp
? ??? lib
? ??? liba.so
??? cmake
? ??? FindLiba.cmake
??? CMakeLists.txt
??? source
??? CMakeLists.txt
??? main.cpp
Run Code Online (Sandbox Code Playgroud)
这liba是一个商业的,开放源代码的第三方库。不幸的是,该库的供应商未为其库提供CMake配置,因此我编写了自己的find模块:
# cmake/FindLiba.cmake
# - Try to find liba
# Once done, this will define
#
# Liba_FOUND - system has liba
# Liba_INCLUDE_DIRS - the liba include directories
# Liba_LIBRARIES - link these to use liba
# Include dir
find_path(Liba_INCLUDE_DIR
NAMES liba.hpp
)
# …Run Code Online (Sandbox Code Playgroud) 我正在从OOP角度编写一个用于C++的Arduino程序并遇到一个问题:类的方法无法看到该类的构造函数中定义的对象.我试图完成的是创建一个对象(类),用于容纳各种方法,这些方法用于计算和输出DHT11传感器的数据.完整代码:
* DhtSensor.h
*
* Created on: 2017-04-18
* Author: Secret
*/
#ifndef DhtSensor_h
#define DhtSensor_h
class DhtSensor {
public:
DhtSensor(int dhtPin); //constructor
void read();
void printToScreen(); //a method for printing to LCD
private:
unsigned long previousMillis; //Millis() of last reading
const long readInterval = 3000; //How often we take readings
float readingData[2][30]; //store current ant last values of temperature [0] and humidity [1] readings
int readingIndex;
bool initDone; //Bool value to check if initialization has been complete …Run Code Online (Sandbox Code Playgroud)