我想将函数/方法标记为已弃用.我试图应用该deprecated属性:
#[deprecated]
fn old_way_of_doing_it() {
Run Code Online (Sandbox Code Playgroud)
但这会产生错误:
Run Code Online (Sandbox Code Playgroud)error: stability attributes may not be used outside of the standard library
有没有一种方法可以让编译器警告我的库的使用者不推荐使用某个函数?
我没有经验,但我正在考虑尝试使用编译器插件和自定义属性,但我想这会要求消费者也使用插件,这可能是不合理的(或者对我来说可能是一项不合理的工作量? )
作为好奇心的一个侧面问题,为什么被弃用的属性仅适用于标准库?
有时检查某些事情是否无法构建是很好的,例如:
// Next line should fail to compile: can't convert const iterator to iterator.
my_new_container_type::iterator it = my_new_container_type::const_iterator();
Run Code Online (Sandbox Code Playgroud)
是否可以将这些类型的东西合并到CMake/CTest中?我正在寻找这样的东西CMakeLists.txt:
add_build_failure_executable(
test_iterator_conversion_build_failure
iterator_conversion_build_failure.cpp)
add_build_failure_test(
test_iterator_conversion_build_failure
test_iterator_conversion_build_failure)
Run Code Online (Sandbox Code Playgroud)
(当然,据我所知,这些特定的CMake指令不存在.)
如果在Ubuntu 12.04上使用Clang 3.2或GCC 4.7进行编译,则以下示例成功运行(即不挂起),但如果我使用VS11 Beta或VS2012 RC进行编译,则会挂起.
#include <iostream>
#include <string>
#include <thread>
#include "boost/thread/thread.hpp"
void SleepFor(int ms) {
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}
template<typename T>
class ThreadTest {
public:
ThreadTest() : thread_([] { SleepFor(10); }) {}
~ThreadTest() {
std::cout << "About to join\t" << id() << '\n';
thread_.join();
std::cout << "Joined\t\t" << id() << '\n';
}
private:
std::string id() const { return typeid(decltype(thread_)).name(); }
T thread_;
};
int main() {
static ThreadTest<std::thread> std_test;
static ThreadTest<boost::thread> boost_test;
// SleepFor(100);
}
Run Code Online (Sandbox Code Playgroud)
问题似乎是,std::thread::join()如果在main …
我想为我正在构建的基于C++的系统创建一个跨平台的安装程序.
我使用CMake构建所有内容,如果我可以使用CPack来制作安装程序,那将会很棒.我已经有CPack在OSX上工作,但我不能让它在Windows上工作.为了简化操作,我尝试在http://www.cmake.org/Wiki/CMake:Packaging_With_CPack上获取示例以使用NSIS安装程序软件.配置完成后,我无法在任何地方找到NSIS安装程序(使用VS 2010 Win64生成器).
也许我很困惑,但我认为只需要源代码,CMake,CPack和NSIS就可以创建安装包而无需Visual Studio.这可能吗?
完整教程的链接(http://www.cmake.org/Wiki/CMake:Component_Install_With_CPack跳过相关信息以获得NSIS工作而不提及生成器或编译器)将非常有用,或者我如何能够获得这个神秘的NSIS安装程序的基本解释会很棒.
以下示例为CMakeLists.txt:
cmake_minimum_required(VERSION 2.6.0 FATAL_ERROR)
project(StPMS)
add_library(mylib mylib.cpp)
add_executable(mylibapp mylibapp.cpp)
target_link_libraries(mylibapp mylib)
install(TARGETS mylib
ARCHIVE
DESTINATION lib
COMPONENT libraries)
install(TARGETS mylibapp
RUNTIME
DESTINATION bin
COMPONENT applications)
install(FILES mylib.h
DESTINATION include
COMPONENT headers)
set(CPACK_GENERATOR NSIS)
set(CPACK_PACKAGE_NAME "MyLib")
set(CPACK_PACKAGE_VENDOR "CMake.org")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "MyLib - CPack Component Installation Example")
set(CPACK_PACKAGE_VERSION "1.0.0")
set(CPACK_PACKAGE_VERSION_MAJOR "1")
set(CPACK_PACKAGE_VERSION_MINOR "0")
set(CPACK_PACKAGE_VERSION_PATCH "0")
set(CPACK_PACKAGE_INSTALL_DIRECTORY "CPack Component Example")
SET(CPACK_NSIS_MODIFY_PATH ON)
INCLUDE(CPack)
Run Code Online (Sandbox Code Playgroud) 我正在尝试在我正在进行的项目中使用新的Boost.Log库.该项目是用CMake构建的.我收到链接错误声称链接器遇到了对Boost.Log的未定义引用
Linking CXX executable main
CMakeFiles/main.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x30): undefined reference to `boost::log::v2s_mt_posix::trivial::logger::get()'
Run Code Online (Sandbox Code Playgroud)
我有一个简单的hello world测试,但没有出现这些错误.如果我链接Boost.Log库会导致它产生未定义的引用错误?
main.cpp中:
#include <boost/log/trivial.hpp>
int main(int argc, char* const argv[]) {
BOOST_LOG_TRIVIAL(info) << "Hello World";
}
Run Code Online (Sandbox Code Playgroud)
的CMakeLists.txt:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
FIND_PACKAGE(Boost 1.54 COMPONENTS log REQUIRED)
FIND_PACKAGE(Threads)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
ADD_EXECUTABLE(main main.cpp)
TARGET_LINK_LIBRARIES(main ${Boost_LOG_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})
Run Code Online (Sandbox Code Playgroud)
编辑:来自cmake和make的详细输出
cmake的:
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:476 ] _boost_TEST_VERSIONS = 1.56.0;1.56;1.55.0;1.55;1.54.0;1.54
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:478 ] Boost_USE_MULTITHREADED = TRUE
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:480 ] Boost_USE_STATIC_LIBS =
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:482 ] Boost_USE_STATIC_RUNTIME =
-- [ /usr/share/cmake-2.8/Modules/FindBoost.cmake:484 ] Boost_ADDITIONAL_VERSIONS = …Run Code Online (Sandbox Code Playgroud) 我对C++比较陌生,我已经看了很多答案,但我从来没有得到满意的答案.
假设我有一个名为的结构FSM.最终在我的代码中,FSM可以创建多个实例.其中一个FSM属性int X不是静态的,每个实例都FSM应该有自己的值X.
现在,其中一个FSM属性是另一个submachine需要读取这样的值的结构X:
struct FSM
{
public:
int x;
int getX(){return x;}
struct submachine
{
void onentry() {int g = getX();};
};
};
Run Code Online (Sandbox Code Playgroud)
这会出现以下错误:
错误:'FSM :: getX':非静态成员函数的非法调用
我的问题是,submachine是一个成员FSM,所以它不应该有权访问所有属性的本地实例FSM吗?如果没有,当我们创建一个实例时FSM,我们不会创建其所有成员的实例,即submachine?如果是这样,那么为什么我们需要创建一个需要的对象onentry()呢?
我假设编译器是正确的,所以我也想知道是否有办法使这项工作.
注意:不幸的是,内部结构(submachine)的实例在调用事件时被实例化,因此我只能定义类型,而不是为它们实例化对象FSM.
我从gcc收到一个奇怪的错误,无法弄清楚原因.我制作了以下示例代码,以使问题更加清晰.基本上,有一个类定义,我为其复制构造函数和复制赋值运算符私有,以防止意外调用它们.
#include <vector>
#include <cstdio>
using std::vector;
class branch
{
public:
int th;
private:
branch( const branch& other );
const branch& operator=( const branch& other );
public:
branch() : th(0) {}
branch( branch&& other )
{
printf( "called! other.th=%d\n", other.th );
}
const branch& operator=( branch&& other )
{
printf( "called! other.th=%d\n", other.th );
return (*this);
}
};
int main()
{
vector<branch> v;
branch a;
v.push_back( std::move(a) );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望这段代码能够编译,但是gcc失败了.实际上gcc抱怨"branch :: branch(const branch&)是私有的",据我所知,不应该调用它.
赋值运算符可以工作,因为如果我用main替换main()的主体
branch a;
branch b; …Run Code Online (Sandbox Code Playgroud) 考虑:
#include <cstdlib>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
class Gizmo
{
public:
Gizmo() : foo_(shared_ptr<string>(new string("bar"))) {};
Gizmo(Gizmo&& rhs); // Implemented Below
private:
shared_ptr<string> foo_;
};
/*
// doesn't use std::move
Gizmo::Gizmo(Gizmo&& rhs)
: foo_(rhs.foo_)
{
}
*/
// Does use std::move
Gizmo::Gizmo(Gizmo&& rhs)
: foo_(std::move(rhs.foo_))
{
}
int main()
{
typedef vector<Gizmo> Gizmos;
Gizmos gizmos;
generate_n(back_inserter(gizmos), 10000, []() -> Gizmo
{
Gizmo ret;
return ret;
});
random_shuffle(gizmos.begin(), gizmos.end());
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,有两个版本Gizmo::Gizmo(Gizmo&&) …
我有一个没有默认构造函数的类,但构造函数可能会抛出.我想要进行如下测试:
EXPECT_THROW(MyClass(param), std::runtime_error);
Run Code Online (Sandbox Code Playgroud)
但编译器g++抱怨没有默认的构造函数MyClass.但是,以下......
EXPECT_THROW(MyClass foo(param), std::runtime_error);
Run Code Online (Sandbox Code Playgroud)
......有效,测试按预期通过.为什么Googletest不会接受临时对象?
class MyClass
{
public:
MyClass(std::string const& filename);
//...
};
Run Code Online (Sandbox Code Playgroud)
有趣的是,我已经重构了我的测试,没有在单独的变量中的文件名,当被要求检查时,我发现以下工作:
EXPECT_THROW(MyClass("somefilename"), std::runtime_error);
Run Code Online (Sandbox Code Playgroud)
但是以下内容不是:
std::string filename("somefilename");
EXPECT_THROW(MyClass(filename), std::runtime_error);
Run Code Online (Sandbox Code Playgroud)