我想在模板类中定义一些模板成员方法,如下所示:
template <typename T>
class CallSometing {
public:
void call (T tObj); // 1st
template <typename A>
void call (T tObj, A aObj); // 2nd
template <typename A>
template <typename B>
void call (T tObj, A aObj, B bObj); // 3rd
};
template <typename T> void
CallSometing<T>::call (T tObj) {
std::cout << tObj << ", " << std::endl;
}
template <typename T>
template <typename A> void
CallSometing<T>::call (T tObj, A aObj) {
std::cout << tObj << ", " << aObj …Run Code Online (Sandbox Code Playgroud) c++ templates arguments compiler-errors template-meta-programming
我正在尝试执行以下操作(仅下面的相关代码部分):
template<typename ContainerType>
struct IsContainerCheck : is_container<ContainerType>
{
static constexpr char* err_value = "Type is not a container model";
};
namespace _check_concept {
template<typename ResultType>
struct run {
constexpr static int apply() {
static_assert(false, IsContainerCheck<ResultType>::err_value)
return 0;
}
};
template<>
struct run<true_t> {
constexpr static int apply() {
return 0;
}
};
}
Run Code Online (Sandbox Code Playgroud)
这会失败,因为static_assert只允许打印文字.与BOOST_STATIC_ASSERT_MSG宏相同.
所以我的问题是 - 在编译期间有没有办法输出constexpr字符串?如果有一个gcc扩展提供这个功能也很棒.
使用的编译器gcc 4.8.1
我正在编写一个模板函数,它应该交换a的两个元素boost::mpl::vector(类似于std::swap).困难的部分是在编译期间没有变量的概念.我写了一个草稿,但我想知道是否有更好的方法来解决这个问题.
我当前的代码草图从迭代器中提取一个整数索引,并使用交换的元素执行序列类型的副本.问题是 - 这可以做得更好:
#include <boost/mpl/distance.hpp>
#include <boost/mpl/begin_end.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/eval_if.hpp>
#include <boost/mpl/comparison.hpp>
#include <boost/mpl/equal.hpp>
#include <boost/mpl/clear.hpp>
#include <boost/mpl/next_prior.hpp>
#include <boost/mpl/push_back.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/or.hpp>
using boost::mpl::distance;
using boost::mpl::begin;
using boost::mpl::end;
using boost::mpl::next;
using boost::mpl::at;
using boost::mpl::or_;
using boost::mpl::int_;
using boost::mpl::eval_if;
using boost::mpl::greater;
using boost::mpl::equal;
using boost::mpl::clear;
using boost::mpl::push_back;
namespace boost { namespace mpl {
template<template<typename, typename> class T, class A, class B>
struct eval2 {
typedef typename T<typename A::type, typename B::type>::type type;
};
namespace …Run Code Online (Sandbox Code Playgroud)
我有一个配置项目:
./main.cpp
./type_traints/TypeTraints.cpp
./type_traints/TypeTraints.hpp
./type_traints/chapter_20.hpp
Run Code Online (Sandbox Code Playgroud)
./type_traints/CMakeLists.txt文件是:
cmake_minimum_required (VERSION 2.8)
add_library(chapter_20 TypeTraints.cpp)
Run Code Online (Sandbox Code Playgroud)
和./CMakeLists.txt如下:
cmake_minimum_required (VERSION 2.8)
project (mpl)
add_subdirectory(type_traints)
include_directories(type_traints)
link_directories(type_traints)
add_executable (mpl main.cpp)
target_link_libraries(mpl chapter_20)
Run Code Online (Sandbox Code Playgroud)
文件的相关部分(大多数包括省略)包括:
./ type_traints /chapter_20.hpp
#ifndef CHAPTER_20_GUARD
#define CHAPTER_20_GUARD
#include <TypeTraints.hpp>
void chapter_20() {
test_23();
}
#endif //CHAPTER_20_GUARD
Run Code Online (Sandbox Code Playgroud)
./type_traints/TypeTraints.hpp
#ifndef TYPE_TRAINTS_GUARD
#define TYPE_TRAINTS_GUARD
namespace details {
template<class T> const char* class2name() {
return "unknown";
};
template<> const char* class2name<int>() {
return "int";
};
}
template<class T>
class type_descriptor {
friend std::ostream& operator << (std::ostream& stream,
const …Run Code Online (Sandbox Code Playgroud) 当尝试在子线程中访问套接字描述符(使用bind()或listen())时,我收到错误:
文件描述符错误
这是代码(标头被忽略):
class task {
private:
std::int32_t socFd;
std::string path;
char buffer[MAX_SOC_BUFFER];
struct sockaddr_un socAddr;
public:
void init() {
if ((socFd = socket((std::int32_t)AF_UNIX, (std::int32_t)SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
socAddr.sun_family = AF_UNIX;
strcpy(socAddr.sun_path, getSocPath().c_str());
}
task(const std::string& path) : path{path}
{
init();
}
auto getSocketFd() ->decltype(socFd) {
return socFd;
}
const std::string& getSocPath() {
return path;
}
auto getSocAddr() -> decltype(socAddr)&
{
return socAddr;
}
char* getBuff() {
return buffer;
}
virtual ~ task() {
close(getSocketFd());
} …Run Code Online (Sandbox Code Playgroud)